-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
85 lines (58 loc) · 1.61 KB
/
Copy pathexample.py
File metadata and controls
85 lines (58 loc) · 1.61 KB
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
from debugly import debug, debugmethods, debugattr
@debug(prefix = '***')
def add(x,y):
return x + y
#if you are using debugmethods we can't wrap if static methods and classmethods are use
@debugmethods
class Spam:
def a(self):
pass
@debug(prefix = '***')#other than static and clasmethods every decorator might work but in the order of classdecorator and func decorator
def b(self):
pass
@debugattr
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
#if we wanted to use a same decorator for many classes with inheritanceslike
#@debugmethods
#class Spam:
#@debugmethods
#class cap(Spam):
#@debugmethods
#class tap(cap):
#then we use metaclass
class debugmeta(type):
def __new_(cls, clsname, bases, clsdict):
clsobj = super().__new_(cls, clsname,
bases, clsdict)
clsobj = debugmethods(clsobj)
return clsobj
#class Base(metaclass = debugmeta):
#class cap(Base)
#Making customized types through inheritence from type class
class mytype(type):
def __new__(cls, clsname, bases, clsdict):
if len(bases) > 1:
raise TypeError("NO!")
return super().__new__(cls, clsname, clsdict)
#to use the custom type we require we need to inherit
#class Base(metaclss = mytype):
# pass
#class A(Base):
# pass
#class B(Base):
# pass
#class C(A,B):
# pass
# Error is raised due to two bases
#Generalized __init__function through inheritence
class Structure:
_fields = []
def __init__(self, *args):
for name, val in zip(self._fields, args):
setatte(self, name, val)
class Stock(Structure):
_fields = ['name', 'shares', 'price']
#Otherwise we have to use init to initialize every attribute