2 ms·
Python name-mangles properties that start with "__" to emulate privacy. Run: class Base: def __init__(self): self.__field = 1 def base
by munificent 8y ago
Python name-mangles properties that start with "__" to emulate privacy. Run:
class Base:
def __init__(self):
self.__field = 1
def baseMethod(self):
print("baseMethod " + str(self.__field))
class Derived(Base):
def __init__(self):
super().__init__()
self.__field = 2
def derivedMethod(self):
print("barMethod " + str(self.__field))
d = Derived()
d.baseMethod()
d.derivedMethod()
And it prints:
baseMethod 1
barMethod 2
Ruby doesn't really have fields that are private to subclasses, but Ruby also embraces monkey-patching, so that's not entirely surprising.