Python Dunder syntax¶
Python Tricks Chapter 2.4
_var¶
- convention: "pseudo" private
- you shouldn't use this private function, var, ...
var_¶
- convention: avoid naming conflicts with built-in Python keywords
__var¶
- name mangling
- really makes it annoying to use that variable outside of the class
class Test:
    def __init__(self):
        self.__ghost = "Casper"
    def get_ghost(self):
        return self.__ghost  # this works!
    def __fn_ghost(self):
        ...
t = Test()
t.__ghost  # AttributeError!
t._Test__ghost  # "Casper", name mangles!
class SuperTest(Test):
    def __init__(self):
        super().__init__()  # no name mangling!
        self.__ghost = "overridden"
st = SuperTest()
st.__ghost  # AttributeError
st._SuperTest__ghost  # "overridden"
st._Test__ghost  # "Casper"
__var__¶
- not name mangled
- convention: reserved for built-in magic methods
_ as a var name¶
- convention: name for a var that you won't use
- Python REPL: the value of the previous expression
  
    
      Last update:
      2023-04-24