def build_profile(first, last, **user_info):
"""Build a dictionary containing everything we know about a user."""
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)
Output:
{'location': 'princeton', 'field': 'physics', 'first_name': 'albert', 'last_name': 'einstein'}
I dont understand why the key and value first_name and last_name was placed at the last? aren't they supposed to be placed before location and field? because of positional arguments??
please help.
In case you'd want the positional arguments passed in to the function to come before the **kwargs
passed in, you could use the dict(..., **kwargs)
approach to build a new dict
object:
def build_profile(first, last, **addl_info):
"""Build a dictionary containing everything we know about a user."""
user_info = {'first_name': first, 'last_name': last, **addl_info}
return user_info
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics')
print(user_profile)
Out:
{'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}