Optional parameters in functions and their mutable default values [duplicate]

Good doc from PyCon a couple years back – Default parameter values explained. But basically, since lists are mutable objects, and keyword arguments are evaluated at function definition time, every time you call the function, you get the same default value.

The right way to do this would be:

def F(a, b=None):
    if b is None:
        b = []
    b.append(a)
    return b

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)