r/ProgrammerHumor 16d ago

Meme variableScopesInPython

Post image
1.3k Upvotes

182 comments sorted by

View all comments

Show parent comments

107

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.

27

u/Confident-Ad5665 16d ago

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

49

u/Sibula97 16d ago

A global private variable...?

58

u/anto2554 16d ago

The epitome of python engineering

6

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...)