Best practice for setting the default value of a parameter that's supposed to be a list in Python? Best practice for setting the default value of a parameter that's supposed to be a list in Python? python python

Best practice for setting the default value of a parameter that's supposed to be a list in Python?


Use None as a default value:

def func(items=None):    if items is None:        items = []    print items

The problem with a mutable default argument is that it will be shared between all invocations of the function -- see the "important warning" in the relevant section of the Python tutorial.


I just encountered this for the first time, and my immediate thought is "well, I don't want to mutate the list anyway, so what I really want is to default to an immutable list so Python will give me an error if I accidentally mutate it." An immutable list is just a tuple. So:

  def func(items=()):      print items

Sure, if you pass it to something that really does want a list (eg isinstance(items, list)), then this'll get you in trouble. But that's a code smell anyway.


For mutable object as a default parameter in function- and method-declarations the problem is, that the evaluation and creation takes place at exactly the same moment. The python-parser reads the function-head and evaluates it at the same moment.

Most beginers asume that a new object is created at every call, but that's not correct! ONE object (in your example a list) is created at the moment of DECLARATION and not on demand when you are calling the method.

For imutable objects that's not a problem, because even if all calls share the same object, it's imutable and therefore it's properties remain the same.

As a convention you use the None object for defaults to indicate the use of a default initialization, which now can take place in the function-body, which naturally is evaluated at call-time.