r/programminghorror • • Oct 26 '26

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

6

u/Beregolas Oct 26 '26

This also happens in JavaScript btw, as well as several smaller scripting languages in game engines, it's a pretty normal feature in many interpreted languages.

In my last backend job, this was actually the source of our longest running and most annoying bug, where a specific page would display the wrong sorting, but never in testing. Because someone *caugh* used a mutable argument to give it default sorting parameters, and they got mutated by a very specific query, which caused it to be wrong for all subsequent requests.

So yeah, this is a real thing, you should know it, and just in case, you should have linting and/or your IDE setup so this kind of thing is rejected out of hand, and cannot be merged to prod/main.

2

u/4xe1 Oct 26 '26

just in case, you should have linting and/or your IDE setup so this kind of thing is rejected out of hand

So what kind of linting have you adopted? Default parameters can be quite handy. Does it reject them, or does it manage to detect and reject their mutations?

4

u/Beregolas Oct 26 '26

I get a warning when I use anything mutable. Instead of an empty list, or a list with presets for example, I have to put None, and set it in code. This is basically also the recommended pattern.

2

u/tehdog 26d ago edited 26d ago

This also happens in JavaScript

No it doesn't? JS has it's own dumbness, but this particular ridicularity is Python-only. e.g. function test(x=[]) { x.push(1); console.log(x) } does what you'd expect.

And this is not the only place where this happens, it also happens in the very-innocent looking.

class Foo:
    x = []
a = Foo()
a.x.append('wat')
b = Foo()
print(b.x)
['wat']

It's the kind of thing that makes you think how could this possibly be a serious programming language if they didn't consider this basic code?

Python is supposed to be simple and intuitive. Nothing is intuitive about having to do def __init__(self): self.x = [] instead of the above.

1

u/Beregolas 26d ago

you actually seem to be correct... I must have misremembered. I vividly remember we had a bug in our Node-JS backend (not my choice) which worked exactly the same as this, but then it was probably passed in or a const...

Sorry for the confusion / misinformation ^^

1

u/tehdog 26d ago

The default parameter syntax in JS is pretty new, up until some years ago you could only use it with TS or transpilation