Using dicts and lists as default arguments of functions

Mel mwilson at the-wire.com
Mon Aug 9 11:31:21 EDT 2010


Johan wrote:

> Dear all,
> 
> Considering this test program:
> 
> def tst(a={}):
>     print 1, a
>     a['1'] = 1
>     print 2, a
>     del a

The idiom to use is 

def tst (a=None):
    if a is None:
        a = {}
        # ...

and so on.  This means that every call to tst with unspecified a creates its 
own empty dict.  Ditto for lists.

	Mel.





More information about the Python-list mailing list