python args & kwargs

Today,We will talk some about the argument and arguments ...

#!/usr/bin/python

def fun(*args):
    for value in args:
        print value


if __name__ == '__main__':
    
    fun(11,22,33,44,55)

What is type of (*args) ?

Do you really want to know ,just keep read ...

#!/usr/bin/python

def fun(*args):
    print type(args)
    for value in args:
        print value


if __name__ == '__main__':
    
    fun(11,22,33,44,55)

Okay,We know the (*args ) just a tuple type?

so,we can input a tuple as argument ...

#!/usr/bin/python

def fun(*args):
    print type(args)
    for value in args:
        print value


if __name__ == '__main__':
    
    my_tuple=(11,22,33,44,55)
    fun(my_tuple) 

Oh,What happened ? The result is no what we expect ...

See the below code,you will find the answer ...

#!/usr/bin/python

def fun(*args):
    print type(args)
    for value in args:
        print value


if __name__ == '__main__':
    
    my_tuple=(11,22,33,44,55)
    fun(*my_tuple)

Okay,see we got the result what we expect ...

Good,time to talk (**kwargs)

#!/usr/bin/python

def fun(**kwargs):
    
    print type(kwargs)
    for key in kwargs:
        print 'key: ',key,'value: ',kwargs[key]
    

if __name__ == '__main__':
    fun(name='Frank',age=23,school='IMUT')

Of course,you can input a dict as argument,but Don't forget the (**) again ...

If you really want to forget (**) ,like the below code ...

#!/usr/bin/python

def fun(**kwargs):
    
    print type(kwargs)
    for key in kwargs:
        print 'key: ',key,'value: ',kwargs[key]
    

if __name__ == '__main__':
    my_dict={'name':'Frank','age':23,'school':'IMUT'}
    fun(my_dict)

You will got a error like the below :

So ,You don't really want to do it again ,right ...haha

#!/usr/bin/python

def fun(**kwargs):
    
    print type(kwargs)
    for key in kwargs:
        print 'key: ',key,'value: ',kwargs[key]
    

if __name__ == '__main__':
    my_dict={'name':'Frank','age':23,'school':'IMUT'}
    fun(**my_dict)

Okay ! see the right result :

But,You may ask when we should use **kwargs ...

and where use it ...

Okay,see example 01 below:

#!/usr/bin/python

class Student(object):
    
    def __init__(self,name):
        super(Student,self).__init__()
        self.name = name


if __name__ == '__main__':
    
    s = Student('Frank')
    print s.name

Okay,keep going ..

sometime ,you may see the below argument usage ...

if __name__ == '__main__':
    
    s = Student('Frank',age=23,id=0011,phone=12345)

But,how it is work inside ...

see the below code ...

#!/usr/bin/python

class Student(object):
    
    def __init__(self,name,**kwargs):
        super(Student,self).__init__()
        self.name = name
        for k in kwargs:
            setattr(self,k,kwargs[k])

if __name__ == '__main__':
    
    s = Student('Frank',age=23,id=0011,phone=12345)
    print s.name
    print s.age
    print s.id
    print s.phone

Thank you !

Can we drop this masquerade
原文地址:https://www.cnblogs.com/landpack/p/4603378.html