r/programminghorror • • 29d ago

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

175

u/drislands 29d ago

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

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

73

u/Xelanybor 29d 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 29d ago

What the fuck

29

u/ZylonBane 29d ago

Which defeats the entire purpose of the default value feature.

29

u/notjfd 28d 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 28d ago

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

5

u/shunabuna 28d 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 26d 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 26d 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 29d ago

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

7

u/bigorangemachine 28d 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 29d ago

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

7

u/ChemTechGuy 28d 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 28d 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 28d ago

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

2

u/trutheality 28d 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 28d ago

Name one language with the same behavior

1

u/Double_Cost4865 28d ago

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

6

u/CaptainFoyle 28d ago edited 27d 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 28d ago

Bob's a dick 

1

u/CaptainFoyle 27d ago

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

I have amended my original comment accordingly.