-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeta_pro_1.py
40 lines (33 loc) · 845 Bytes
/
Meta_pro_1.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
import math
class CustomType(type):
def __new__(cls, name, base, class_dict):
print('Customized type creation!')
cls_obj = super().__new__(cls, name, base, class_dict)
cls_obj.circ = lambda self: 2*math.pi*self.r
return cls_obj
class_body = """
def __init__(self, x,y,r):
self.x = x
self.y= y
self.r = r
def area(self):
return math.pi*self.r**2
"""
class_dict = {}
exec(class_body, globals(), class_dict)
Circle_1 = CustomType('Circle', (), class_dict)
print(Circle_1)
type(Circle_1)
print(isinstance(Circle_1, type))
c_1 = Circle_1(0,0,1)
print(c_1.area())
class Circle_2(metaclass=CustomType):
def __init__(self, x, y, r):
self.x = x
self.y = y
self.r = r
def area(self):
return math.pi* self.r**2
c_2 = Circle_2(0,0,2)
print(c_2.area())
print(c_2.circ())