博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python args & kwargs
阅读量:5269 次
发布时间:2019-06-14

本文共 2862 字,大约阅读时间需要 9 分钟。

 

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

#!/usr/bin/pythondef fun(*args):    for value in args:        print valueif __name__ == '__main__':        fun(11,22,33,44,55)

 

What is type of (*args) ?

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

#!/usr/bin/pythondef fun(*args):    print type(args)    for value in args:        print valueif __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/pythondef fun(*args):    print type(args)    for value in args:        print valueif __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/pythondef fun(*args):    print type(args)    for value in args:        print valueif __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/pythondef 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/pythondef 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/pythondef 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/pythonclass Student(object):        def __init__(self,name):        super(Student,self).__init__()        self.name = nameif __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/pythonclass 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 !

转载于:https://www.cnblogs.com/landpack/p/4603378.html

你可能感兴趣的文章
使用xrdp或Xmanager 远程连接 CentOS6
查看>>
SEH简单研究
查看>>
Linux误删恢复
查看>>
Unity调用Windows窗口句柄,选择文件和目录
查看>>
HashMap循环遍历方式
查看>>
React Native 入门 调试项目
查看>>
C# 通过 Quartz .NET 实现 schedule job 的处理
查看>>
关于java之socket输入流输出流可否放在不同的线程里进行处理
查看>>
目前为止用过的最好的Json互转工具类ConvertJson
查看>>
XHTML学习要点
查看>>
JavaScript的学习要点
查看>>
我用到的 Linq 扩展方法
查看>>
Day13
查看>>
tensorflow saver简介+Demo with linear-model
查看>>
Luogu_4103 [HEOI2014]大工程
查看>>
1043: [HAOI2008]下落的圆盘 - BZOJ
查看>>
线程同步之读写锁
查看>>
codeforces 620D Professor GukiZ and Two Arrays
查看>>
pylint
查看>>
Oracle——SQL基础
查看>>