Python keyword arguments unpack and return dictionary
Question:
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.
Answers:
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
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()
If you don’t want to pass **kwargs
, you can simply return locals
:
def generate_student_dict(first_name=None, last_name=None,
birthday=None, gender=None):
return locals()
Note that you want to remove self
from the result if you pass it as an argument.
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.
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}
To convert keyword only arguments to dict you can easily use this helper
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.
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
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()
If you don’t want to pass **kwargs
, you can simply return locals
:
def generate_student_dict(first_name=None, last_name=None,
birthday=None, gender=None):
return locals()
Note that you want to remove self
from the result if you pass it as an argument.
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.
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}
To convert keyword only arguments to dict you can easily use this helper