Today,we some about the argument and arguments ...
# !/usr/bin/python def Fun (*args): for in args: print value if __name__ ' __main__ ' : Fun (11,22,33,44,55)
What is type of (*args)?
Do your really want to know, just keep read ...
# !/usr/bin/python def Fun (*args): print type (args) for 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 in args: Print value if __name__ ' __main__ ' : my_tuple= (11,22,33,44,55)
Oh,what happened? The result is no-what we expect ...
See the below code,you'll find the answer ...
# !/usr/bin/python def Fun (*args): print type (args) for 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/pythondefFun (* *Kwargs):Printtype (Kwargs) forKeyinchKwargs: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/pythondefFun (* *Kwargs):Printtype (Kwargs) forKeyinchKwargs:Print 'Key:', Key,'Value:', Kwargs[key]if __name__=='__main__': My_dict={'name':'Frank',' Age': 23,'School':'Imut'} fun (my_dict)
You'll got a error like the below:
So, you don ' t really want-do it again, right ... haha
#!/usr/bin/pythondefFun (* *Kwargs):Printtype (Kwargs) forKeyinchKwargs: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:
Thank you!
Python args & Kwargs