-
Notifications
You must be signed in to change notification settings - Fork 3
/
010_meta_classes.py
49 lines (37 loc) · 911 Bytes
/
010_meta_classes.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
class Person:
pass
person = Person()
# Objekt je objekt
print(type(person))
print(type(3))
# Trieda je objekt typu type
print(type(Person))
print(type(int))
# A type je typu type
print(type(type))
# Ako sa rodia triedy? pomocou meta triedy type :O
# type(<name>, <bases>, <dct>):
# meno
# zoznam rodicov Tuple
# zoznam menneho priestoru ... teda premennych a metod Dictionary
A = type("A", (), {})
a = A()
print(a)
# class A:
# pass
def multiline_method(self):
print(f"X is {self.x}")
B = type("B", (A,), {"x": 10, "get_x": lambda self: self.x, "multiline_method": multiline_method})
b = B()
print(b)
print(b.get_x())
b.multiline_method()
# Vlastne Meta classy
# factory class .... Nikto to nepouziva :D
class Meta(type):
def __init__(cls, name, bases, dict):
print("New class is born")
cls.Pi = 3.14
class Circle(metaclass=Meta):
pass
# print(Circle.Pi)