Python array as parameter list

I have an array that matches function parameters:

        TmpfieldNames = []
        TmpfieldNames.append(Trademark.name)
        TmpfieldNames.append(Trademark.id)
        return func(Trademark.name, Trademark.id)

func(Trademark.name.Trademark.id)works but func(TmpfieldNames)does not work. How can I call a function without explicitly indexing into an array, for example func(TmpfieldNames[0], TmpfieldNames[1])?

+5
source share
3 answers

With, *you can unpack the arguments from listor tupleand **unpack the arguments from dict.

>>> range(3, 6)             # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)            # call with arguments unpacked from a list
[3, 4, 5]

An example from the documentation .

+20
source

I think you are looking for:

def f(a, b):
    print a, b

arr = [1, 2]
f(*arr)
+11
source

What are you looking for:

func(*TmpfieldNames)

But this is not a typical use case for such a function; I assume you created it for demonstration.

+1
source

All Articles