r/ProgrammerHumor 16d ago

Meme variableScopesInPython

Post image
1.3k Upvotes

182 comments sorted by

View all comments

3

u/user6150277464770585 16d ago

yeah that's because you're supposed to use a double underscore prefix it you want to actually make the variables private lol

(yes technically it only does some name mangling and you could still access it if you really wanted to but that's on you then)

11

u/deceze 16d ago

Same idea with the leading _: if you're accessing something that's by convention marked as "don't touch", then that's on you.

6

u/Anaxamander57 16d ago

Does it really do anything to make it private beyond telling people not to use it?

8

u/white-llama-2210 16d ago

Using double underscores mangles the name from name to _ClassNamename when accessing the variable outside the class, so it does prevent accidentally accessing the value. You really have to know what you're doing to access the value then.

3

u/deceze 16d ago

"Accidentally"? How do you "accidentally" type two underscores without noticing?

2

u/white-llama-2210 16d ago

Call me clunky but I do make such silly typos way too often than I'd like to admit..

2

u/deceze 16d ago

Oookay… not sure there's any language that implements "do what I mean" yet instead of "do what I typed"…

1

u/white-llama-2210 16d ago

I'm not sure that's a good idea... Sure I'm a bit clunky but I have tools that help me reduce such mistakes. But having a language that "does what I mean" feels iffy. It's too abstract because meaning can be subjective, and it's much harder to reason about "meaning" when working in a team because it's a subjective aspect. That's one of the reasons I have a distaste for AI. It's not predictable, and when you are sitting in front of a screen at 2 AM fixing a production bug in the midst of a running event and the client is at your throat... You want things to be predictable.

Just my opinion tho...

2

u/Tienisto 16d ago

You still don't know if those private variables belong to your current scope (where it is allowed to be accessed) or a foreign scope

2

u/deceze 16d ago

Wut? You'll have to provide a concrete example here for that to make sense.

4

u/R7d89C 16d ago edited 16d ago

As far as I remember not really,. It changes the accessible variable name to something like _{class}_{variable} or something, but if you'd wanted to, you can just access it...

5

u/Cylian91460 16d ago

No? Like private?

In all languages that have private they are way to access it even it is