函数传参总结

#__author__:Administrator}
#data: 2017/11/14
#必须参数
# def print_info(name,age):
# print('name:%s'%name)
# print('age:%d'%age)
#
# print_info('xuxu',25)

#关键字参数
# def print_info(name,age):
# print('name:%s'%name)
# print('age:%d'%age)
#
# print_info(age=25,name='xuxu')

#默认参数,一定要跟在其他参数后面
# def print_info(name,age,sex='boy'):
# print('name:%s'%name)
# print('age:%d'%age)
# print('sex:%s' % sex)

# print_info('xuxu',25)
# print_info('xuxu1',25)
# print_info('xuxu3',25,'girls')

#不定长参数
#例子1
# def add(*m):
# sam = 0
# for i in m:
# sam += i
# print(sam)
#
# add(1,2,3,4,5)
#例子2
# def print_info(*args,**kwargs):
# print(args)
# #print(kwargs)
# for i in kwargs:
# print('%s:%s'%(i,kwargs[i]))
#
# print_info('xuxu','xuxu2',name = 'xuxu',age = 23,sex = 'boy')
结论:
#关于不定长参数的位置,*args放左边,**kwargs放右边

#如果有默认参数放左边
#def(name,age = 25,*args,**kwargs)优先级

posted @ 2017-11-14 16:59  无敌帅帅头  阅读(93)  评论(0编辑  收藏  举报