m
Size: a a a
m
m
# Creating a class
class A:
# Declaring public method
def fun(self):
print("Public method of class A")
# Declaring protected method
def _fun(self):
print("Protected method of class A")
self.__fun()
# Declaring private method
def __fun(self):
print("Private method of class A")
class B(A):
# Declaring public method
def fun(self):
print("Public method of class B")
self._fun()
# Declaring private method
def __fun(self):
print("Private method of class B")
obj = B()
obj.fun()
>>Public method of class B
>>Protected method of class A
>>Private method of class A
m
obj._fun()
>>Protected method of class A
>>Private method of class A
m
obj.__fun()
>>AttributeError: 'B' object has no attribute '__fun'
m
obj._A__fun()
>>Private method of class A
obj._B__fun()
>>Private method of class B
m
print(B.__dict__)то есть при создании словаря объекта B и A - имя метода которое начинается с двойного подчеркивания становится
>>{'__module__': '__main__', 'fun': <function B.fun at 0x0000017768917620>, '_B__fun': <function B.__fun at 0x0000017768B17268>, '__doc__': None}
_classname__methodname
m
obj.__fun = obj.__getattribute__('_A__fun')
obj.__fun()
>>Private method of class A
m
obj.__fun = obj.__getattribute__('_A__fun')
print(obj.__dict__)
{'__fun': <bound method A.__fun of <__main__.B object at 0x000002AFC36F8470>>}
😍
😍
O
O
E
O
ДК
# Creating a class
class A:
# Declaring public method
def fun(self):
print("Public method of class A")
# Declaring protected method
def _fun(self):
print("Protected method of class A")
self.__fun()
# Declaring private method
def __fun(self):
print("Private method of class A")
class B(A):
# Declaring public method
def fun(self):
print("Public method of class B")
self._fun()
# Declaring private method
def __fun(self):
print("Private method of class B")
obj = B()
obj.fun()
>>Public method of class B
>>Protected method of class A
>>Private method of class A
ДК
m
OM