Reputation: 2665
I have a function definition as below and I am passing keyword arguments. How do I get to return a dictionary with the same name as the keyword arguments?
Manually I can do:
def generate_student_dict(first_name=None, last_name=None , birthday=None, gender =None):
return {
'first_name': first_name,
'last_name': last_name,
'birthday': birthday,
'gender': gender
}
But I don't want to do that. Is there any way that I can make this work without actually typing the dict?
def generate_student_dict(self, first_name=None, last_name=None, birthday=None, gender=None):
return # Packed value from keyword argument.
Upvotes: 23
Views: 7250
Reputation: 149
In case you want to return the updated values as dictionary
def generate_student_dict(first_name=None, last_name=None , birthday=None, gender =None):
main_args = locals().copy()
first_name = 'first'
last_name = 'last'
birthday = '20 jan 1993'
newvar = 100
new_args = locals().copy()
updated_args = {}
for k,v in new_args.items():
if k in main_args.keys():
updated_args[k] = v
return updated_args
dict = generate_student_dict()
print(dict) #{'first_name': 'first', 'last_name': 'last', 'birthday': '20 jan 1993', 'gender': None}
In case you want to return the iniital values as dictionary
def generate_student_dict_1(first_name=None, last_name=None , birthday=None, gender =None):
args = locals()
first_name = 'first'
last_name = 'last'
birthday = '20 jan 1993'
newvar = 100
return args
dict = generate_student_dict_1()
print(dict) #{'first_name': None, 'last_name': None, 'birthday': None, 'gender': None}
if you want to include all the changes including extra variables defined inside the method
def generate_student_dict_2(first_name=None, last_name=None , birthday=None, gender =None):
first_name = 'first'
last_name = 'last'
birthday = '20 jan 1993'
newvar = 100
args = locals()
return args
dict = generate_student_dict_2()
print(dict) #{'first_name': 'first', 'last_name': 'last', 'birthday': '20 jan 1993', 'gender': None, 'newvar': 100}
Upvotes: -1
Reputation: 22021
If that way is suitable for you, use kwargs (see Understanding kwargs in Python) as in code snippet below:
def generate_student_dict(self, **kwargs):
return kwargs
Otherwise, you can create a copy of params with built-in locals()
at function start and return that copy:
def generate_student_dict(first_name=None, last_name=None , birthday=None, gender =None):
# It's important to copy locals in first line of code (see @MuhammadTahir comment).
args_passed = locals().copy()
# some code
return args_passed
generate_student_dict()
Upvotes: 14
Reputation: 989
You can use locals()
function. locals()
updates and returns a dictionary representing the current local symbol table. Free variables are returned by locals()
when it is called in function blocks, but not in class blocks.
Upvotes: 0
Reputation: 5174
Get keyword arguments in **kwargs
def generate_student_dict(self, **kwargs):
# use it like
# kwargs.get('first_name')
# kwargs.get('last_name')
# kwargs.get('birthday')
# kwargs.get('gender')
return kwargs
Upvotes: 3