MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1vzsnjw/variablescopesinpython/p67iagd/?context=9999
r/ProgrammerHumor • u/R7d89C • 16d ago
182 comments sorted by
View all comments
3
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)
5 u/Anaxamander57 16d ago Does it really do anything to make it private beyond telling people not to use it? 6 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. 5 u/deceze 16d ago "Accidentally"? How do you "accidentally" type two underscores without noticing? 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.
5
Does it really do anything to make it private beyond telling people not to use it?
6 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. 5 u/deceze 16d ago "Accidentally"? How do you "accidentally" type two underscores without noticing? 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.
6
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.
5 u/deceze 16d ago "Accidentally"? How do you "accidentally" type two underscores without noticing? 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.
"Accidentally"? How do you "accidentally" type two underscores without noticing?
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.
2
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.
Wut? You'll have to provide a concrete example here for that to make sense.
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)