forked from pimiento/oop_webinar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.py
108 lines (80 loc) · 2.04 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from dataclasses import dataclass
from typing import ClassVar
def birthday(cat):
cat["age"] += 1
cat_type = {
"init": add_cat,
"name": None,
"color": None,
"age": None,
"birthday": birthday
}
cats = []
cats.append(cat_type.copy())
cats[0]["name"] = "Барсик"
cats[0]["color"] = "рыжий"
cats[0]["age"] = 3
def add_cat(cats, params):
cats.append(cat_type.copy())
cats[-1].update(params)
birthday(cats[0])
@dataclass
class Animal(object):
sound: ClassVar[str] = None
color: str
age: float
def voice(self): # self == a_cat
print(self.sound)
@dataclass
class Cat(Animal):
tail_state: ClassVar[str] = "puffed up"
__sleep_counter: ClassVar[float] = 0
name: str
def birthday(self, months=None):
if months is None:
self.age += 1
else:
self.age += months/12
def sleep(self, hours):
self.__sleep_counter += hours
self.__sleep_counter %= 24
@property
def sleep_counter(self):
return self.__sleep_counter
def __repr__(self):
return f"{self.name} {self.color} {self.age}"
@dataclass
class Bird(Animal):
can_fly: bool
a_cat = Cat("рыжик", "рыжий", 4)
a_cat2 = Cat("Вася", "чёрный", 3)
Cat.tail_state = "down"
class A:
def function(self):
print("— Я твой отец, Люк!")
class B(A):
def function(self):
print("— Я Люк!")
super().function()
"""
KISS — Keep It Simple Stupid (оставь это простым, дубина)
DRY — Don't Repeat Yourself (не повторяй самого себя)
Работает не трожь!
"""
class AbstractAnimal:
def sound(self):
raise NotImplementedError
def what_is_it(self: AbstractAnimal):
return f"{self.__class__.__name__} {self.color} {self.age}"
class X:
var = 1
@classmethod
def foo(cls):
print(cls)
class YMixin:
@staticmethod
def bar():
return "Bar"
class Z(X, YMixin):
def foobar(self):
return self.var