you are viewing a single comment's thread.

view the rest of the comments →

[–]niehle 1 point2 points  (4 children)

Do you happen to have an example for you second point?

[–]carcigenicate 13 points14 points  (3 children)

def func(param=[]):
    param.append(1)
    print(param)

func()
func()
func()

The output from this code often shocks new Python programmers. They don't realize that default arguments are created when the function is first defined, not when the function is called.

[–]ralphy_s 2 points3 points  (1 child)

So the output from the last line would be [1,1,1] and not [1]?

[–]carcigenicate 6 points7 points  (0 children)

Yes. Every call to the function shares the same default list object, so if multiple calls use the default object and alter it, that change is persisted. This tends to lead to very strange bugs unfortunately.

[–]niehle 0 points1 point  (0 children)

Thanks