r/programminghorror • • Oct 26 '26

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

Show parent comment

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