zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    java性能优化之HashMap,LinkedHashMap,TreeMap读取大量数据效率的比较
    jdk8 stream实现sql单表select a,b,sum(),avg(),max() from group by a,b order by a,b limit M offset N及其性能
    postgresql cstore_fdw性能测试
    Oracle JDBC prefetch: how to avoid running out of RAM
    mysql-创建用户报错ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'localhost'
    kafka外部访问设置
    mysql 排序长度限制之max_length_for_sort_data以及mysql两种排序算法
    mybatis三个执行器的差别
    Dubbo的集群容错与负载均衡策略及自定义(一致性哈希路由的缺点及自定义)
    mysql中包含长字段索引的优化
  • 原文地址:https://www.cnblogs.com/landpack/p/4603378.html
Copyright © 2011-2022 走看看