You'll need to import sys for that to work.

#!/usr/bin/python

import sys
print  sys.argv[1:]

Example

:/tmp% cat foo.py
#!/usr/bin/python

import sys
print (sys.argv[1:]);

:/tmp% python foo.py 'hello world' arg3 arg4 arg5
['hello world', 'arg3', 'arg4', 'arg5']
Answer from Filip Roséen on Stack Overflow
🌐
Bobby Hadz
bobbyhadz.com › blog › python-get-all-arguments-passed-to-function
Get all arguments passed to a Function in Python | bobbyhadz
Use the `locals()` function to get all arguments passed to a function, e.g. `all_arguments = locals()`.
Discussions

Getting all arguments from a function call in python
If you for some reason want to get a list of all the arguments, but you don't want to have a variable amount with *args and **kwargs, then you can save the locals at the beginning of the function and use that. Example: >>> def test(a, b, c): ... args = locals() ... print(args) ... >>> test(1, 2, 3) {'a': 1, 'b': 2, 'c': 3} More on reddit.com
🌐 r/learnpython
13
3
May 23, 2021
python - How do I access command line arguments? - Stack Overflow
I use python to create my project settings setup, but I need help getting the command line arguments. I tried this on the terminal: $python myfile.py var1 var2 var3 In my Python file, I want to u... More on stackoverflow.com
🌐 stackoverflow.com
How to get complete list of arguments for function from module in python - Stack Overflow
I am using iPython in command prompt, Windows 7. I thought this would be easy to find, I searched and found directions on how to use the inspect package but it seems like the inspect package is me... More on stackoverflow.com
🌐 stackoverflow.com
python - Accessing all function argmuments - Stack Overflow
I have a function with 4 arguments and want to check those 4 arguments for something. Currently I do it like this: def function1(arg1, arg2, arg3, arg4): arg1 = function2(arg1) arg2 = func... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 11
184

Essentially, I'm trying to work out how to, from within a function, get a completely populated *args and **kwargs, including the function's named parameters.

How about saving the arguments via locals() at the beginning of the function?

def my_func(a, *args, **kwargs):
    saved_args = {**locals()}  # Updated to make a copy per loco.loop
    print("saved_args is", saved_args)
    local_var = 10
    print("saved_args is", saved_args)
    print("But locals() is now", locals())
    
my_func(20, 30, 40, 50, kwarg1='spam', kwarg2='eggs')

It gives this output:

saved_args is {'a': 20, 'args': (30, 40, 50), 'kwargs': {'kwarg1': u'spam', 'kwarg2': u'eggs'}}
saved_args is {'a': 20, 'args': (30, 40, 50), 'kwargs': {'kwarg1': u'spam', 'kwarg2': u'eggs'}}
But locals is now {'a': 20, 'saved_args': {...}, 'args': (30, 40, 50), 'local_var': 10, 'kwargs': {'kwarg1': u'spam', 'kwarg2': u'eggs'}}

Hat tip: https://stackoverflow.com/a/3137022/2829764

2 of 11
58

Not something I'd do, but you could use inspect.signature to introspect the arguments your method takes:

>>> import inspect
>>> def foobar(foo, bar, baz):
...     return inspect.signature(foobar)
... 
>>> foobar(1, 2, 3)
<Signature (foo, bar, baz)>

The returned Signature instance has an ordered collection of parameters (the .parameters attribute) which can then be used together with locals() to produce a list of your argument values:

>>> def foobar(foo, bar, baz):
...     sig, foobar_locals = inspect.signature(foobar), locals()
...     return [foobar_locals[param.name] for param in sig.parameters.values()]
...
>>> foobar(1, 2, 3)
[1, 2, 3]

However, you really only need such magic when doing advanced function decorators and the like. I think it's overkill here.

🌐
CodeRivers
coderivers.org › blog › python-get-arguments
Python Get Arguments: A Comprehensive Guide - CodeRivers
February 22, 2026 - In Python, you can access command-line arguments using the sys module. The sys.argv list contains all the arguments passed to the script when it is run from the command line.
🌐
Medium
moez-62905.medium.com › the-ultimate-guide-to-command-line-arguments-in-python-scripts-61c49c90e0b3
The Ultimate Guide to Command Line Arguments in Python Scripts | by Moez Ali | Medium
April 18, 2023 - In Python, command-line arguments are accessed through the sys.argv list. The first item in the list is always the name of the script itself, and subsequent items are the arguments passed to the script.
🌐
w3resource
w3resource.com › python-exercises › python-basic-exercise-76.php
Python: Command line arguments - w3resource
Write a Python program to count the number of command-line arguments passed to a script. Write a Python program to check if a specific argument exists in the command-line input. Write a Python program to accept multiple file names as command-line arguments and display their content. Write a Python program to validate numerical command-line arguments and perform a mathematical operation. ... Previous: Write a Python program to get the copyright information and write Copyright information in Python code.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › command-line-arguments-in-python
Command Line Arguments in Python - GeeksforGeeks
The simplest way to access command line arguments in Python is through the sys module. It provides a list called sys.argv that stores everything you type after python in the command line. sys.argv: A list in Python that contains all the command-line ...
Published: December 17, 2025
🌐
Hyperskill
hyperskill.org › university › python › command-line-arguments-in-python
Command Line Arguments in Python — sys.argv & argparse Guide
August 10, 2026 - In Python, these arguments can be accessed using the sys.argv list, where sys.argv[0] is the script name, and subsequent elements are the passed arguments. To access command line arguments, import the sys module and use sys.argv, a list where the first element is the script name, and the following ...
Top answer
1 of 2
6

Since you have a set number of arguments just create an iterable out of them, for example, wrap the argument names in a tuple literal:

for arg in (arg1, arg2, arg3, arg4):
    # do stuff

If you don't mind your function being capable of being called with more args just use *args syntax:

def function1(*args):
    for arg in args:
        arg = function2(arg)

which then exposes the arguments with which the function was invoked as a tuple which can be iterated over.

If you need to store the results for every invocation, it is better to change the for-loop in a list comprehension creating a list of the returned values. For example, given a function that takes a value and simply adds 2 to it:

def add2(arg):
    return arg + 2

def function1(arg1, arg2, arg3, arg4):
    a1, a2, a3, a4 = [add2(arg) for arg in (arg1, arg2, arg3, arg4)]
    print(a1, a2, a3, a4)

We create a list comprehension that takes every arg and supplies it to add2 and then stores it as an entry in a list, then we unpack to a1,..,aN. The values now contain the results of the function calls:

function1(1, 2, 3, 4)
3, 4, 5, 6

In the previous examples (arg1, arg2, arg3, arg4) can always be replaced with args if you use the *args syntax when defining the function, the results will be similar.


As an addendum, if you're more a fan of functional programming, you could always map the arguments to the function and save a few characters :-). I'll add a *args definition of function1 this time:

def function1(*args):
    a1, a2, a3, a4 = map(add2, args)
    print(a1, a2, a3, a4)

Same result.

2 of 2
3

Yes, use *args:

In [1]: def func(*args):
   ...:     print(len(args), args)
   ...:     

In [2]: func(1, 2, 3, 4)
4 (1, 2, 3, 4)

More information can be found here.

🌐
Python
docs.python.org › 3.3 › library › argparse.html
16.4. argparse — Parser for command-line options, arguments and sub-commands — Python 3.3.7 documentation
All parameters should be passed as keyword arguments. Each parameter has its own more detailed description below, but in short they are: prog - The name of the program (default: sys.argv[0]) usage - The string describing the program usage (default: generated from arguments added to parser)
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-get-list-of-parameters-name-from-a-function-in-python
How to get list of parameters name from a function in Python? - GeeksforGeeks
July 23, 2025 - Explanation: inspect.getfullargspec(fun) retrieves detailed information about the function’s arguments and accessing .args extracts only the parameter names as a list. ast module allows analyzing a function’s source code to extract parameter names. This method is useful for static code analysis, especially when working with dynamically loaded scripts, but is not efficient for direct function inspection. ... import ast source = """ def my_function(a, b, c=10): pass """ tree = ast.parse(source) func_node = tree.body[0] # assuming the first function in the script res = [arg.arg for arg in func_node.args.args] print(res)
🌐
Tutorialspoint
tutorialspoint.com › python › python_command_line_arguments.htm
Python - Command-Line Arguments
Python provides a getopt module that helps you parse command-line options and arguments. This module provides two functions and an exception to enable command line argument parsing.
🌐
InformIT
informit.com › articles › article.aspx
4.11 Accessing Command-Line Arguments | 22 Python Tricks | InformIT
July 31, 2019 - For example, here is a program named silly.py that does nothing but print all the arguments given to it, including the program name. import sys for thing in sys.argv: print(thing, end=' ') ... The following example gives a more sophisticated way to use these strings, by converting them to floating-point format and passing the numbers to the quad function.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-command-line-arguments
Python Command Line Arguments: sys.argv, argparse, getopt | DigitalOcean
March 25, 2026 - Learn Python command line arguments with sys.argv, argparse, and getopt. Compare patterns, handle bad input, and copy the examples.
🌐
Python Morsels
pythonmorsels.com › accessing-command-line-arguments-python
Accessing command-line arguments in Python - Python Morsels
July 17, 2026 - Python is just ignoring whatever argument we passed to this program. Python stores all the arguments that we give it, but it doesn't do anything with the arguments: it's up to us (the implementer of this program) to do something with them.