Python tutorial explains it:
import sys
print(sys.argv)
More specifically, if you run python example.py one two three:
>>> import sys
>>> print(sys.argv)
['example.py', 'one', 'two', 'three']
Answer from SilentGhost on Stack OverflowBest way to pass arguments to a function that requires a large number of inputs?
python - How do I access command line arguments? - Stack Overflow
Some way to get arguments of current function for passing along to another function even when not using *args/**kw
How to pass through Python args and kwargs? - Stack Overflow
For example, suppose I had a function which required 50 arguments and these arguments were stored in an object with 500 properties.
I could simply pass the object to the function, but then I’d be passing an addition 450 values that are not needed by the function. This feels like bad practice, but I’m not aware of whether or not that’s true.
EDIT: 50 args does indeed sound excessive (and I’ll admit that it’s a slight exaggeration). However, quite a lot of arguments are taken by this function because it is responsible for aggregating results from other models. In addition to taking in lots of inputs, there are also various aggregation settings that the user may specify.
Python tutorial explains it:
import sys
print(sys.argv)
More specifically, if you run python example.py one two three:
>>> import sys
>>> print(sys.argv)
['example.py', 'one', 'two', 'three']
I highly recommend argparse which comes with Python 2.7 and later.
The argparse module reduces boiler plate code and makes your code more robust, because the module handles all standard use cases (including subcommands), generates the help and usage for you, checks and sanitize the user input - all stuff you have to worry about when you are using sys.argv approach. And it is for free (built-in).
Here a small example:
import argparse
parser = argparse.ArgumentParser("simple_example")
parser.add_argument("counter", help="An integer will be increased by 1 and printed.", type=int)
args = parser.parse_args()
print(args.counter + 1)
and the output for python prog.py -h
usage: simple_example [-h] counter
positional arguments:
counter counter will be increased by 1 and printed.
optional arguments:
-h, --help show this help message and exit
and the output for python prog.py 1 As one would expect:
2
The * and ** operators are used in two different situations.
When used as part of a function definition,
def save_name_for(self, *args, **kwargs):it is used to signify an arbitrary number of positional or keyword arguments, respectively. The point to remember is that inside the function
argswill be a tuple, andkwargswill be a dict.When used as part of a function call,
args = (1, 2) kwargs = {'last': 'Doe', 'first': 'John'} self.save_name_for(*args, **kwargs)the
*and**act as unpacking operators.argsmust be an iterable, andkwargsmust be dict-like. The items inargswill be unpacked and sent to the function as positional arguments, and the key/value pairs inkwargswill be sent to the function as keyword arguments. Thus,self.save_name_for(*args, **kwargs)is equivalent to
self.save_name_for(1, 2, last='Doe', first='John')
See also the saltycrane blog for an explanation with examples.
You pass them with syntax mirroring the argument syntax:
self.save_name_for(*args, **kwargs)
Note that you do not need to pass in self; save_name_for is already bound.