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

前端 未结 4 1081
情歌与酒
情歌与酒 2020-12-29 02:02

I have a Python function that takes a list as a parameter. If I set the parameter\'s default value to an empty list like this:

def func(items=[]):
    print          


        
相关标签:
4条回答
  • 2020-12-29 02:30

    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.

    0 讨论(0)
  • 2020-12-29 02:33

    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.

    0 讨论(0)
  • 2020-12-29 02:45

    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.

    0 讨论(0)
  • 2020-12-29 02:48

    In addition and also to better understand what python is, here my little themed snippet:

    from functools import wraps
    def defaultFactories(func):
        'wraps function to use factories instead of values for defaults in call'
        defaults = func.func_defaults
        @wraps(func)
        def wrapped(*args,**kwargs):
            func.func_defaults = tuple(default() for default in defaults)
            return func(*args,**kwargs)
        return wrapped
    
    def f1(n,b = []):
        b.append(n)
        if n == 1: return b
        else: return f1(n-1) + b
    
    @defaultFactories
    def f2(n,b = list):
        b.append(n)
        if n == 1: return b
        else: return f2(n-1) + b
    
    >>> f1(6)
    [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]
    >>> f2(6)
    [1, 2, 3, 4, 5, 6]
    
    0 讨论(0)
提交回复
热议问题