What behavior is expected from the following code?
Python performs compile-time name mangling for any identifier inside a class body that begins with two underscores and has at most one trailing underscore, rewriting it to _ClassName__identifier; this applies uniformly to class-level attributes and to attributes assigned through self inside methods. The class attribute originally written as __Var is therefore stored as _Class__Var with value 2, and the instance attribute assigned as self.__prop inside init is stored as self._Class__prop with value 4, so accessing both under their mangled names and adding them together yields 6. Attributes prefixed with only a single underscore, such as _Var and self._prop, are merely a naming convention and are not mangled, remaining accessible under their original names.
Community Discussion