首页 > IT职业 > IT职场 > python关键字实参的使用

说明
1、在形参名前加两个星号**,表示创建一个名为形参的空字典,用来存储任意数量的键值对。
2、形参名**user_info中的两个星号允许Python创建一个名为user_info的空字典,并将收到的所有名称值对放入字典。
实例
#使用任意数量的关键字实参
def build_profile(first, last, **user_info):
'''创建一个字典,其中包含我们知道的有关用户的一切。'''
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('albert', 'einstein',
location= 'princeton',
field= 'physics')
print(user_profile)
# >>> {'location': 'princeton', 'field': 'physics', 'first_name': 'albert', 'last_name': 'einstein'}
以上就是python关键字实参的使用,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3356/viewspace-2828839/ ,如需转载,请注明出处,否则将追究法律责任。