标签:参数
1.定义一个函数fun(*args),该函数的效果如下:
fun(222,111,‘xian‘,‘hahahha‘) --》‘xian‘
fun(7,‘name‘,‘fensi‘) -->‘fensi‘
fun(1,2,3,4) -->None
做法如下:
def fun(*args):
a=args[2]
if type(a)==int:
return None
else:
return a
li=222,111,‘xian‘,‘hahahah‘
lin=[1,2,3]
zhang=[7,‘name‘,‘fensi‘]
print fun(*li)
print fun(*lin)
print fun(*zhang)
print fun(1,2,3,4)
2.定义一个func(name=None,**kwargs),该函数的效果为:
func(‘liyulong‘) -->name:‘liyulong‘
func(‘liyulong‘,age=22) --> "name:liyulong, age:22"
func(‘liyulong‘,age=22,‘weight=50) --> "name:liyulong,age:22,weight:50"
做法如下:
def func(name=None,**kwargs):
print ‘name:‘+name
for k,w in kwargs.items():
print ‘%s : %s‘%(k,w)
print type(kwargs)
func(‘liyulong‘)
func(‘liyulong‘,age=22)
func(‘liyulong‘,age=22,weight=50)
标签:参数
原文地址:http://12920735.blog.51cto.com/12910735/1951240