r/programminghorror • • Oct 26 '26

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

175

u/drislands Oct 26 '26

I would have expected this function to create a new empty list on any call where items is not given. Doesn't this behavior make the function into a closure?

189

u/nakurtag Oct 26 '26

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 Oct 26 '26

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

73

u/Xelanybor Oct 26 '26

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 Oct 26 '26

What the fuck

29

u/ZylonBane Oct 26 '26

Which defeats the entire purpose of the default value feature.

29

u/notjfd Oct 26 '26

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 Oct 26 '26

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

5

u/shunabuna Oct 26 '26

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 27d 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 28d 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 Oct 26 '26

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

7

u/bigorangemachine 29d 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 Oct 26 '26

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

7

u/ChemTechGuy Oct 26 '26

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 29d 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 29d ago

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

2

u/trutheality Oct 26 '26

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 Oct 26 '26

Name one language with the same behavior

1

u/Double_Cost4865 Oct 26 '26

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

6

u/CaptainFoyle Oct 26 '26 edited 28d ago

Of course. This is just a very stupid way of writing the function. Initiate with None and Bob's your uncle a dick.

2

u/rasputin1 29d ago

Bob's a dick 

1

u/CaptainFoyle 28d ago

He might be! Uncles sometimes are, or so I've been informed.

I have amended my original comment accordingly.