Take the 2-minute tour ×

If I declare a function with non keyword arguments such as a tuple and keyword arguments such as a dictionary, are they declared?

For example:

def someFunc(a, *nkw, **kwa):
    nkwList = []
    kwList  = []
    for i in nkw:
    for j in kwa:
    print a, nkwList, kwList




1 [] []

As you can see, even though I did not pass a tuple and a dictionary, I didnot get Index out of range error when I loop through nkw and kwa. From my understanding, I think *nkw and **kwa are created in the function declaration itself.

Can anyone help me understand this concept?

share|improve this question
note that *nkw and **kwa are conventionally called *args and **kwargs –  second May 27 '12 at 10:38

2 Answers 2

up vote 1 down vote accepted

The tuple and dictionary are always created, even if they are empty (i.e. nothing was passed in them).

share|improve this answer
Thanks for the reply. –  user907629 May 27 '12 at 10:27

nkw and kwa are of course passed as emty list/dict. Doing something else would completely defy their purpose, as you could never access them safely, you't always have to check if they exist.

Your probably confusing this witht the default parameters. For example:

def someFunc(x=[]):

Here the list used as default for x is instantiated on declaration of the function, an therfore is the same on all invocations of the function.

share|improve this answer
Thanks for the answer. My question is not about passing an empty list or dict or my question is not about doing something with them or accessing them. To clarify, are the parameters used in the function declaration are created. Refer to @Ignacio Vazquez-Abrams answer. You will understand. –  user907629 May 27 '12 at 10:32

Your Answer


