Last updated: Apr 10, 2024
Reading timeยท6 min
Use the locals()
function to get all arguments passed to a function, e.g.
all_arguments = locals()
.
The locals()
function will return a dictionary that contains all of the
passed to the function arguments.
def func(first, *args, **kwargs): # ๐๏ธ ['first', 'args', 'kwargs'] print(list(locals().keys())) all_arguments = locals() # ๐๏ธ {'first': 'hello', 'args': ('arg1', 'arg2'), 'kwargs': {'name': 'bobby', 'age': 30}} print(all_arguments) my_variable = 'bobbyhadz.com' # ๐๏ธ {'first': 'hello', 'args': ('arg1', 'arg2'), 'kwargs': {'name': 'bobby', 'age': 30}, 'all_arguments': {...}, 'my_variable': 'bobbyhadz.com'} print(locals()) func('hello', 'arg1', 'arg2', name='bobby', age=30)
We used the locals()
function inside of func
to get all of the arguments
that were passed to the function.
locals()
function returns a dictionary that contains the current scope's local variables.One thing to note is that you should use store the output of the locals
dictionary in a variable before declaring any other variables.
def func(first, *args, **kwargs): print(list(locals().keys())) # ๐๏ธ ['first', 'args', 'kwargs'] # ๐๏ธ at the top of your function ๐๏ธ all_arguments = locals() func('hello', 'arg1', 'arg2', name='bobby', age=30)
The line must be at the top of your function definition because the locals
dictionary contains all of the current scope's local variables.
In other words, if you declare a variable in the function before using
locals()
, the variable will be contained in the dictionary.
def func(first, *args, **kwargs): # ๐๏ธ ['first', 'args', 'kwargs'] print(list(locals().keys())) all_arguments = locals() # ๐๏ธ {'first': 'hello', 'args': ('arg1', 'arg2'), 'kwargs': {'name': 'bobby', 'age': 30}} print(all_arguments) my_variable = 'bobbyhadz.com' # ๐๏ธ {'first': 'hello', 'args': ('arg1', 'arg2'), 'kwargs': {'name': 'bobby', 'age': 30}, 'all_arguments': {...}, 'my_variable': 'bobbyhadz.com'} print(locals()) func('hello', 'arg1', 'arg2', name='bobby', age=30)
Notice that the my_variable
variable is contained in the locals()
dictionary
in the print() output at the bottom.
You can also use the dict.keys()
and dict.values()
methods on the locals()
dictionary.
def func(first, *args, **kwargs): # ๐๏ธ ['first', 'args', 'kwargs'] print(list(locals().keys())) # ๐๏ธ ['hello', ('arg1', 'arg2'), {'name': 'bobby', 'age': 30}] print(list(locals().values())) all_arguments = locals() # ๐๏ธ {'first': 'hello', 'args': ('arg1', 'arg2'), 'kwargs': {'name': 'bobby', 'age': 30}} print(all_arguments) func('hello', 'arg1', 'arg2', name='bobby', age=30)
If you need to get the names of a function's parameters in a list, you can also
use the inspect.signature()
method.
import inspect def func(first, *args, **kwargs): # ๐๏ธ ['first', 'args', 'kwargs'] print(list(locals().keys())) # ๐๏ธ ['hello', ('arg1', 'arg2'), {'name': 'bobby', 'age': 30}] print(list(locals().values())) all_arguments = locals() # ๐๏ธ {'first': 'hello', 'args': ('arg1', 'arg2'), 'kwargs': {'name': 'bobby', 'age': 30}} print(all_arguments) print(inspect.signature(func)) # ๐๏ธ (first, *args, **kwargs) # ๐๏ธ ['first', 'args', 'kwargs'] print(list(inspect.signature(func).parameters.keys())) func('hello', 'arg1', 'arg2', name='bobby', age=30) # ๐๏ธ (first, *args, **kwargs) print(inspect.signature(func)) # ๐๏ธ ['first', 'args', 'kwargs'] print(list(inspect.signature(func).parameters.keys()))
The
inspect.signature()
method returns a Signature
object for the given callable.
The Signature.parameters
attribute is an ordered mapping of the function's
parameters.
You can use list(inspect.signature(func).parameters.keys())
to get the
function's parameter names in a list.
You can also use the inspect.signature()
method if you need to get the values
of all the arguments that were passed to the function.
import inspect def func(first, *args, **kwargs): all_arguments = locals() signature_values = inspect.signature(func).parameters.values() all_argument_values = [ all_arguments[parameter.name] for parameter in signature_values ] # ๐๏ธ ['hello', ('arg1', 'arg2'), {'name': 'bobby', 'age': 30}] print(all_argument_values) func('hello', 'arg1', 'arg2', name='bobby', age=30)
We used a
list comprehension to
only get the values for the parameter keys from the locals()
dictionary.
To pass all arguments of a function to another function:
*args
syntax to gather all positional arguments.**kwargs
syntax to gather all keyword arguments.def concat(first, second, third): return first + second + third def funcA(*args, **kwargs): print(args) # ๐๏ธ ('bobby', 'hadz', '.com') print(kwargs) # ๐๏ธ {} result = concat(*args, **kwargs) print(result) return result funcA('bobby', 'hadz', '.com') # ๐๏ธ bobbyhadz.com
The funcA
function uses the *args
syntax to gather all positional arguments
into a tuple and the **kwargs
argument to gather all keyword arguments into a
dictionary.
We didn't pass any keyword arguments to the function in the previous example, but here is an example that uses keyword arguments as well.
def concat(first, second, third): return first + second + third def funcA(*args, **kwargs): print(args) # ๐๏ธ ('bobby',) print(kwargs) # ๐๏ธ {'second': 'hadz', 'third': '.com'} result = concat(*args, **kwargs) print(result) return result funcA('bobby', second='hadz', third='.com') # ๐๏ธ bobbyhadz.com
The second='hadz'
and third='.com'
keyword arguments get grouped into the
kwargs
dictionary.
The last step is to use the
iterable unpacking *
operator to unpack the tuple and the dictionary unpacking **
operator to
unpack the dictionary in the call to the concat
function.
*
operator unpacks the provided iterable and passes its elements as multiple, comma-separated arguments to the function.The following two function calls achieve the same result.
args = ['bobby', 'hadz'] concat(*args) # ------------------------- concat('bobby', 'hadz')
The following two function calls also achieve the same result.
kwargs = {'first': 'bobby', 'last': 'hadz'} concat(**kwargs) # --------------------------- concat(first='bobby', last='hadz')
*args
You can also specify other positional arguments before the *args
syntax in the
function's definition.
def concat(first, second, third): return first + second + third def funcA(example_arg, *args, **kwargs): print(example_arg) # ๐๏ธ example arg print(args) # ๐๏ธ ('bobby',) print(kwargs) # ๐๏ธ {'second': 'hadz', 'third': '.com'} result = concat(*args, **kwargs) print(result) return result funcA('example arg', 'bobby', second='hadz', third='.com') # ๐๏ธ bobbyhadz.com
Note that the example_arg
argument comes before *args
and **kwargs
.
locals()
Alternatively, you can use the locals()
function.
The locals()
function returns a dictionary that contains the current scope's
local variables.
def concat(first, second, third): return first + second + third def funcA(first, second, third): # ๐๏ธ {'first': 'bobby', 'second': 'hadz', 'third': '.com'} print(locals()) result = concat(**locals()) print(result) return result funcA('bobby', 'hadz', '.com') # ๐๏ธ bobbyhadz.com
The example uses the locals()
function to get a dictionary that contains the
current scope's local variables.
We used the dictionary unpacking **
operator to unpack the dictionary in the
call to the concat
function.
funcA
and concat
have to take the same arguments because the arguments of funcA
get passed as keyword arguments to concat
.Since the local()
dictionary contains all of the current scope's local
variables, make sure to not declare any variables before you call the
function.
Otherwise, your locally declared function variables would also get passed in the function call.
A much better approach is to be explicit and write out the arguments in the function call.
def concat(first, second, third): return first + second + third def funcA(first, second, third): result = concat(first, second, third) print(result) return result funcA('bobby', 'hadz', '.com') # ๐๏ธ bobbyhadz.com
We explicitly passed the arguments of funcA
to concat
.
However, if you don't have to use the arguments in funcA
, a better approach
would be to not pollute the function's definition and use the iterable unpacking
*
operator.
def concat(first, second, third): return first + second + third def funcA(example_arg, *args): print(example_arg) # ๐๏ธ example arg print(args) # ๐๏ธ ('bobby', 'hadz', '.com') result = concat(*args) print(result) return result # ๐๏ธ bobbyhadz.com funcA('example arg', 'bobby', 'hadz', '.com')
We used the *args
syntax to gather the positional arguments and used the
iterable unpacking *
operator to unpack them in the call to the concat
function.
The benefit of doing this is that the funcA
function no longer has to define
the same arguments as concat
to only redirect them.
You can learn more about the related topics by checking out the following tutorials: