r/programminghorror • • Oct 26 '26

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

Show parent comment

2

u/tehdog 27d 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