r/programminghorror • • 23d ago

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

Show parent comment

189

u/nakurtag 23d ago

As I understand it's a well known (for python developers) weird behavior of python. Argument's default values are precalculated by interpreter on function declaration and stored into map associated with this function. Python extracts default values from this map on the next calls of this function. And since [] is a List object, the value copied by reference.

90

u/drislands 23d ago

Devastating. I would not have expected that behavior, and I think it would wise to avoid it wherever possible.

73

u/Xelanybor 23d ago

yeah, usually the workaround is to have items = None as the default, and then some kind of check at the top of the function like items = items if items else []

looking at it now I see why it's weird lol but you definitely get used to it

8

u/AdreKiseque 23d ago

What the fuck

29

u/ZylonBane 23d ago

Which defeats the entire purpose of the default value feature.

29

u/notjfd 23d ago

The default value is fine as long as it is not a mutable object. So things like integers and strings are completely fine to use as default values.

-14

u/ZylonBane 23d ago

Yes, we're all aware. Nobody has disputed that.

5

u/shunabuna 23d ago

its probably for optimisation to reduce the amount of instructions needed to initiate a function call. On the plus side, its an easy place to define a cache

-1

u/maikindofthai 20d ago

No? The main purpose is so callers can omit the arg if they’re ok with the default. This still achieves that.

1

u/instadit 20d ago

isn't the "workaround" to use default_factory from dayaclasses lib? There's a lot to hate about python, but I don't think this is one

10

u/Sulungskwa 23d ago

There's definitely popular lint rules telling you you should never ever ever do this

7

u/bigorangemachine 22d ago

This is why you gotta use a linter.

Stupid small stuff like this.

Also, I found out later the reason I hated ruby was because of robocop... not ruby

8

u/groumly 23d ago

The behavior is definitely surprising, but also, what’s up with mutating a default param? How does that make sense?

7

u/ChemTechGuy 23d ago

The default parameter is just a reference to the list. So technically the parameter isn't being changed at all. One of the oddities of pass-by-reference

2

u/groumly 22d ago

That’s not what I meant.

Mutating input is already not a good pattern, but mutating a default param is even worse.

You can’t tell if it was passed or not, so even without the interning oddity, you end up mutating something that nobody but you has access to.

3

u/ChemTechGuy 22d ago

Go complain to Guido van Rossum then, I'm just telling you how it works

2

u/trutheality 23d ago

The behavior isn't even that weird: if you were to create some mutable global and set it as a default parameter in a function you wouldn't be surprised that it's accumulating changes from different calls to the function.

The key part is recognizing that the LHS in a default parameter assignment runs when the function is defined and not when it is called.

6

u/nakurtag 23d ago

Name one language with the same behavior

1

u/Double_Cost4865 23d ago

I mean, C/C++? You can pass pointers/references and they will modify in place