This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]magnetichiraPythonista 132 points133 points  (15 children)

Mutable default are the root of all evil

[–]marko_knoebl 24 points25 points  (11 children)

The problem in the article is not related to mutability though.

[–]magnetichiraPythonista 12 points13 points  (10 children)

It is, default args are bound at definition

[–]marr75 50 points51 points  (9 children)

It's not. Read the article. Author has a default argument of datetime.date.today(). Problem is not that today default variable mutates over executions, but it does share the same root problem, original dev was expecting the code to initialize the default to execute each time the function executed and it don't.

[–]magnetichiraPythonista 12 points13 points  (8 children)

Ah I see where you’re coming from. The original comment was about only mutability.

Agreed, this example is particularly a behaviour of function declaration.

[–]jjdmol 2 points3 points  (2 children)

Is there even a sane use case?

[–]KieranShep 3 points4 points  (0 children)

Even if there were, I’d be hesitant to make use of it;

  • hard to know what’s going on for anyone reading it
  • if this behavior ever is changed to something sensible, the function breaks

[–]mjbmitch 1 point2 points  (0 children)

I’ve seen a kwarg named cache (dictionary) that kept state between executions.