4 ms·
Why does the _MyClass__number example work the way it does?
by seasonalgrit 9y ago
Why does the _MyClass__number example work the way it does?
- hroman 9y agohttps://docs.python.org/3/tutorial/classes.html#private-variables https://docs.python.org/3/tutorial/classes.html#private-vari...
- masklinn 9y agoWithin class bodies, symbols prefixed with two underscores get "mangled" (prefixed with the class name). This is intended to avoid risks of collisions for classes built specifically for inheritance so you'd usually use it with attributes e.g. `self.__name = name` (that way if a subclass defines its own "name" things will work as expected for both). I was unaware this also worked for straight local variables. I was unaware it happened for all symbols, but I guess it makes sense as the Python compiler does very little analysis.