123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- class Animal:
- def says(self):
- return 'I speak!'
-
- class Horse(Animal):
- def says(self):
- return 'Neigh!'
-
- class Donkey(Animal):
- def says(self):
- return 'Hee-haw!'
-
- class Mule(Donkey, Horse):
- pass
- class Hinny(Horse, Donkey):
- pass
- def test_animals():
- mule = Mule()
- hinny = Hinny()
- print(mule.says())
- print(hinny.says())
- print(Mule.mro())
- print(Mule.__mro__)
- # Миксины
-
- class Super():
- def dump(self):
- print('Super!!!')
- class PrettyMixin():
- def dump(self):
- import pprint
- pprint.pprint(vars(self))
- class Thing(Super, PrettyMixin):
- pass
- def test_mixin():
- t = Thing()
- t.name = "Nyarlathotep"
- t.feature = 'ichor'
- t.age = 'eldritch'
- t.dump()
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Инструкция property
-
- class Duck():
- def __init__(self, input_name) -> None:
- self.hidden_name = input_name
- def get_name(self):
- print('inside the getter')
- return self.hidden_name
-
- def set_name(self, input_name):
- print('inside the setter')
- self.hidden_name = input_name
- name = property(get_name, set_name)
- class ImprovedDuck():
- def __init__(self, input_name) -> None:
- self.__name = input_name
- @property
- def name(self):
- print('inside the getter')
- return self.__name
-
- @name.setter
- def name(self, input_name):
- print('inside the setter')
- self.__name = input_name
- def set_property():
- # don = Duck('Donald')
- don = ImprovedDuck('Donald')
- print(don.name)
- don.name = 'Donna'
- print(don.name)
- print(don._ImprovedDuck__name)
- # set_property()
-
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Методы классов @classmethod
-
- class A():
- count = 0
- def __init__(self):
- A.count += 1
- def exclaim(self):
- print('I`m an A!')
- @classmethod
- def kids(cls):
- print("A has", cls.count, "little objects.")
- def test_classmethod():
- easy_a = A()
- breezy_a = A()
- wheezy_a = A()
- A.kids()
- # test_classmethod()
-
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Статические методы @staticmethod
-
- class CoyoteWeapon():
- @staticmethod
- def commercial():
- print('This CoyoWeapon has been brought to you by Acme')
- def test_staticmethod():
- CoyoteWeapon.commercial()
- # test_staticmethod()
|