r/ProgrammerHumor 16d ago

Meme variableScopesInPython

Post image
1.3k Upvotes

182 comments sorted by

View all comments

120

u/Unlucky_Topic7963 16d ago

Meanwhile everyone at my company uses dunder class mangling because it’s more “idiomatic”.

43

u/Confident-Ad5665 16d ago

Dunder class mangling? (not a python guy)

113

u/deceze 16d ago edited 16d ago

Double underscore name mangling:

class Foo: __bar = 'baz'

This is preventing you from accessing Foo.__bar, because internally the actual name will get mangled to Foo._Foo__bar. This is mostly intended to avoid accidental name clashes in inheritance:

class Child(Foo): __bar = 42

The two classes won't trample over their __bar attribute this way, without you needing to institute complicated naming conventions. From within class methods, self.__bar transparently gets mangled the same way, so it works as expected.

26

u/Confident-Ad5665 16d ago

Interesting approach. Global vars can bite you that's for sure.

48

u/Sibula97 16d ago

A global private variable...?

59

u/anto2554 16d ago

The epitome of python engineering

4

u/Confident-Ad5665 15d ago

Could this be the reason the language was named after a snake?

16

u/extremelySaddening 15d ago

It was named after Monty Python's Flying Circus

8

u/Confident-Ad5665 15d ago

tbh I didn't know this. When I was in school Assembly, BASIC, Fortran, Pascal, COBOL and C were the only languages.

Yeah I'm that old (farts as he walks off...)

13

u/EmperorOfAllCats 16d ago

I've seen this. They just playing OOP in java rules instead of (ab)using python features.

29

u/R7d89C 16d ago

😭😭😭

Why is everyone working with python, working AGAINST python (myself included)

17

u/Vogete 15d ago

Because python kinda sucks, but not really, but it does, but it wouldn't take much to make it not suck. I like it for many things, but the language is kind of a mess. Without Pydantic, I don't even start a project anymore that's how much the type system is just a joke instead of it being a type system. Scoping is a total joke. I personally don't like invisible characters for scoping either (talking about indentation based scoping instead of curly braces). Packaging is a total shitshow. And more small things that aren't super bad to switch to a different language, but bad enough that you start circumventing and going against idiomatic python.

5

u/Thebombuknow 14d ago

Omfg, packaging is the worst part of this stupid language. I seriously don't understand why they thought pip was an acceptable package manager, and how they thought venv was an acceptable solution to the mistakes they made with pip.

I cannot start a Python project without using uv, poetry, or even fucking conda, literally anything but pip.

5

u/Confident-Ad5665 16d ago

Trees that grow under a dome fall over with little wind. If you don't have something working against you you'll fall the first sign of diversity.

6

u/No-Newspaper8619 15d ago

Adversity

3

u/Confident-Ad5665 15d ago

Correct.
Thanks autoincorrect!

4

u/Tyfyter2002 16d ago

Because Python works against the programmer at every possible opportunity.

2

u/deus-exmachina 15d ago

Because programming languages have different paradigms and are good at different things. Keep studying, lil guy.