Get all arguments passed to a Function in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

  1. Get all arguments passed to a Function in Python
  2. Getting the names of a function's parameters in a list
  3. Getting the values of all function's arguments
  4. Pass all arguments of a function to another function
  5. Pass all arguments of function to another function using locals()

# Get all arguments passed to a Function in Python

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.

main.py
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)

get all arguments passed to function

The code for this article is available on GitHub

We used the locals() function inside of func to get all of the arguments that were passed to the function.

The locals() function returns a dictionary that contains the current scope's local variables.

# Use the locals() function before declaring other 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.

main.py
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)

use locals function before declaring other variables

The code for this article is available on GitHub

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.

main.py
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.

main.py
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)

using keys and values methods on the locals dictionary

The code for this article is available on GitHub

# Getting the names of a function's parameters in a list

If you need to get the names of a function's parameters in a list, you can also use the inspect.signature() method.

main.py
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 code for this article is available on GitHub

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.

# Getting the values of all function's arguments

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.

main.py
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)
The code for this article is available on GitHub

We used a list comprehension to only get the values for the parameter keys from the locals() dictionary.

# Python: Pass all arguments of a function to another function

To pass all arguments of a function to another function:

  1. Use the *args syntax to gather all positional arguments.
  2. Use the **kwargs syntax to gather all keyword arguments.
  3. Use the iterable unpacking operator and the dictionary unpacking operator when calling the function.
main.py
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 code for this article is available on GitHub

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.

main.py
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 code for this article is available on GitHub

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.

You can imagine that the iterable unpacking * 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.

main.py
args = ['bobby', 'hadz'] concat(*args) # ------------------------- concat('bobby', 'hadz')

The following two function calls also achieve the same result.

main.py
kwargs = {'first': 'bobby', 'last': 'hadz'} concat(**kwargs) # --------------------------- concat(first='bobby', last='hadz')

# Specifying other positional arguments before *args

You can also specify other positional arguments before the *args syntax in the function's definition.

main.py
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
The code for this article is available on GitHub

Note that the example_arg argument comes before *args and **kwargs.

# Pass all arguments of function to another function using locals()

Alternatively, you can use the locals() function.

The locals() function returns a dictionary that contains the current scope's local variables.

main.py
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.

This means that 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.

# It's better to explicitly write out the function's arguments

A much better approach is to be explicit and write out the arguments in the function call.

main.py
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
The code for this article is available on GitHub

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.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev