python args & kwargs
2015-06-26 22:45
686 查看
Today,We will talk some about the argument and arguments ...
What is type of (*args) ?
Do you really want to know ,just keep read ...
Okay,We know the (*args ) just a tuple type?
so,we can input a tuple as argument ...
Oh,What happened ? The result is no what we expect ...
See the below code,you will find the answer ...
Okay,see we got the result what we expect ...
Good,time to talk (**kwargs)
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 ...
You will got a error like the below :
So ,You don't really want to do it again ,right ...haha
Okay ! see the right result :
But,You may ask when we should use **kwargs ...
and where use it ...
Okay,see example 01 below:
Okay,keep going ..
sometime ,you may see the below argument usage ...
But,how it is work inside ...
see the below code ...
Thank you !
#!/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 !
相关文章推荐
- python函数参数(可变参数tuple和关键字参数list)
- hashlib
- Pig python udf简介
- Python学习笔记-小记
- Python学习心得实例(2)
- Python单元测试unittest加载方式之二:加载测试套件
- python之模块安装
- Python单元测试unittest加载方式之一:unittestmain()启动单元测试模块
- python之列表常见操作
- python之环境搭建windows版
- Python学习笔记_Python基础
- 如果Python中有很多换行,可以选择使用"""..."""表示多行内容
- python中转义用法 r''
- 介绍一个python的新的web framework——karloop框架
- python中的三个读read(),readline()和readlines()
- python对json的相关操作
- VS2013+Python+OpenCV的配置
- python中的深拷贝和潜拷贝
- python读取TXT到数组及列表去重后按原来顺序排序的方法
- python编程练习