1. 程式人生 > >Python之*args和**kwargs使用方法

Python之*args和**kwargs使用方法

*args **kwargs

Python *args使用方法:

#!/usr/bin/env python
#-*- coding=utf-8 -*-

def args(args,*kwargs):
    print (args)
    for arg in kwargs:
        print (arg)
if __name__ == "__main__":
    args('hello',"python","變量","111",111)

運行結果:

[root@bogon code]# python test.py 
hello
python
變量
111
111

Python **kwargs使用方法:

#!/usr/bin/env python
#-*- coding=utf-8 -*-

def greet_me(**kwargs):
	for key,value in kwargs.items():
		print key,value
if __name__ == "__main__":
	my_info={'name':'gsw','email':'[email protected]'}
	greet_me(**my_info)	#傳字典參數
	greet_me(name="gsw",qq='948691540',email="[email protected]")	#傳多鍵值對參數

運行結果:

[root@bogon code]# python test.py 
name gsw
email [email protected]
qq 948691540
name gsw
email [email protected]


Python之*args和**kwargs使用方法