Accessing all function argmuments
<p>I have a function with 4 arguments and want to check those 4 arguments for something.
Currently I do it like this:</p>

<pre><code>def function1(arg1, arg2, arg3, arg4):
    arg1 = function2(arg1)
    arg2 = function2(arg2)
    arg3 = function2(arg3)
    arg4 = function2(arg4)

def function2(arg):
    arg = dosomething(arg)
    return arg
</code></pre>

<p>I think this is not really a nice way to do it, so my idea is to do something like this:</p>

<pre><code>def function1(arg1, arg2, arg3, arg4):
    for arg in listOfArguments:
        arg = function2(arg)


def function2(arg):
    arg = checkSomething(arg)
    return arg
</code></pre>

<p>Is there a way in Python to get a list of all the arguments passed to <code>function1</code>?</p>

<p>Thanks for your help and ideas.</p>

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

<pre><code>for arg in (arg1, arg2, arg3, arg4):
    # do stuff
</code></pre>

<p>If you don't mind your function being capable of being called with more args just use <a href="https://docs.python.org/3/reference/compound_stmts.html#function-definitions" rel="nofollow"><code>*args</code> syntax</a>:</p>

<pre><code>def function1(*args):
    for arg in args:
        arg = function2(arg)
</code></pre>

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

<p>If you need to store the results for every invocation, it is better to change the <code>for-loop</code> in a <a href="https://docs.python.org/3.6/tutorial/datastructures.html#list-comprehensions" rel="nofollow"><em>list comprehension</em></a> creating a list of the returned values. For example, given a function that takes a value and simply adds <code>2</code> to it:</p>

<pre><code>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)
</code></pre>

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

<pre><code>function1(1, 2, 3, 4)
3, 4, 5, 6
</code></pre>

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

<hr>

<p>As an addendum, if you're more a fan of functional programming, you could always <strong><a href="https://docs.python.org/3/library/functions.html#map" rel="nofollow"><code>map</code></a></strong> the arguments to the function and save a few characters :-). I'll add a <code>*args</code> definition of <code>function1</code> this time:</p>

<pre><code>def function1(*args):
    a1, a2, a3, a4 = map(add2, args)
    print(a1, a2, a3, a4)
</code></pre>

<p>Same result.</p>

python function python-3.x