将函数编写成能够接受任意数量的键-值对——调用语句提供类多少就接受多少,直接看下面例子:

 1 >>> def build_profile(first,last,**user_info):
 2 ...     profile = {}
 3 ...     profile['first_name'] = first
 4 ...     profile['last_name'] = last
 5 ...     for key,value in user_info.items():
 6 ...             profile[key] = value
 7 ...     return profile
 8 ... 
 9 >>> user_profile = build_profile('albert','einstein',location='princeton',field='physics')
10 >>> print(user_profile)
11 {'last_name': 'einstein', 'location': 'princeton', 'first_name': 'albert', 'field': 'physics'}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2021-08-24
  • 2021-10-01
  • 2022-12-23
  • 2021-07-06
  • 2021-11-08
猜你喜欢
  • 2021-06-18
  • 2022-12-23
  • 2022-01-27
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案