我正在从一本书中学习python(Eric Matthes的python速成课程) 但是我发现这个代码我不可能理解。你知道吗
def build_profile(first, last, **user_info):
"""Build a dictionary containing everything we know about a user."""
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics')
print(user_profile)
您可以查看this GeeksforGeeks article了解更多详细信息。简而言之,
**user_info
是一个dict
,它包含作为键值对传递的任何其他命名参数。所以为了将它包含在profile
dict中,我们在它的items()
上循环。你知道吗例如,如果我调用
build_profile('Steve', 'William', age=24)
,您将看到user_info
将是{'age': 24}
。你知道吗在您包含的示例中,它将是
{'location':'princeton', 'field':'physics'}
。你知道吗相关问题 更多 >
编程相关推荐