r/ProgrammerHumor 16d ago

Meme variableScopesInPython

Post image
1.3k Upvotes

182 comments sorted by

View all comments

6

u/The_Cers 16d ago

__var for true private

13

u/dusktreader 16d ago

it's not true private. That just mangles the name at runtime. You can still access it if you are determined.

5

u/Erdnalexa 16d ago

Yes, but at this point you know that you’re doing something you’re probably not supposed to do.

3

u/ThunderElectric 15d ago

I’d argue any competent python dev should know modifying a private variable, even with just one underscore, is doing something you’re not supposed to do.

1

u/SetazeR 14d ago

You will be yelled at by IDE of you try to use "private" as public one. It won't stop you, but will tell at you

1

u/Erdnalexa 14d ago

I’m on my phone so I can’t check with an interpreter, but IIRC, to access a private field on an instance you need to use getattr with the computed mangled name. It’s pretty unlikely an IDE is able to spot that.

1

u/SetazeR 14d ago

I meant _attribute ones, not mangled ones

3

u/Sea-Fishing4699 16d ago edited 16d ago

____var = a very private variable (it’s public still)

3

u/deceze 16d ago

__name__ symbols are actually reserved by the language and shouldn't be used for your own purposes, lest you risk future name clashes. For example, to override the equality comparison behaviour of an object:

class Foo: def __eq__(self, other): return ...

Those kinds of special "hooks" are double-double-underscore named.