Files
Learn-Python-Programming-Th…/ch06/oop/private.attrs.fixed.py
T
adii1823 b6eb3ef8a7 ch06
2021-10-28 17:38:47 +05:30

31 lines
688 B
Python

# oop/private.attrs.fixed.py
class A:
def __init__(self, factor):
self.__factor = factor
def op1(self):
print('Op1 with factor {}...'.format(self.__factor))
class B(A):
def op2(self, factor):
self.__factor = factor
print('Op2 with factor {}...'.format(self.__factor))
obj = B(100)
obj.op1() # Op1 with factor 100...
obj.op2(42) # Op2 with factor 42...
obj.op1() # Op1 with factor 100... <- Wohoo! Now it's GOOD!
print(obj.__dict__.keys())
# dict_keys(['_A__factor', '_B__factor'])
"""
$ python private.attrs.fixed.py
Op1 with factor 100...
Op2 with factor 42...
Op1 with factor 100...
dict_keys(['_A__factor', '_B__factor'])
"""