-
Notifications
You must be signed in to change notification settings - Fork 206
/
classmethod_vs_staticmethod.py
179 lines (124 loc) · 3.13 KB
/
classmethod_vs_staticmethod.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import itertools
import pathlib
import ipaddress
import multiprocessing.pool
import importlib.abc
import datetime
from dataclasses import dataclass
class StaticMethod:
def __init__(self, func):
self.func = func
def __get__(self, instance, owner):
return self.func
def __call__(self, *args, **kwargs): # New in Python 3.10
return self.func(*args, **kwargs)
class ClassMethod:
def __init__(self, func):
self.func = func
def __get__(self, instance, owner):
return self.func.__get__(owner, type(owner))
class A:
def normal(self, *args, **kwargs):
print(f"normal({self=}, {args=}, {kwargs=})")
@staticmethod
def f1(*args, **kwargs):
print(f"f1({args=}, {kwargs=})")
@StaticMethod
def f2(*args, **kwargs):
print(f"f2({args=}, {kwargs=})")
@classmethod
def g1(cls, *args, **kwargs):
print(f"g1({cls=}, {args=}, {kwargs=})")
@ClassMethod
def g2(cls, *args, **kwargs):
print(f"g2({cls=}, {args=}, {kwargs=})")
@staticmethod
def h1(*args, **kwargs):
print(f"h1({args=}, {kwargs=})")
@StaticMethod
def h2(*args, **kwargs):
print(f"h2({args=}, {kwargs=})")
@classmethod
def j(cls, *args, **kwargs):
print(f"j({cls=}, {args=}, {kwargs=})")
@dataclass
class Matrix:
shape: tuple[int, int]
@staticmethod
def can_multiply(a, b):
n, m = a.shape
k, l = b.shape
return m == k
@staticmethod
def can_multiply(*matrices):
"""Does m0 @ m1 @ ... @ mn make sense?"""
for a, b in itertools.pairwise(matrices):
n, m = a.shape
k, l = b.shape
if m != k:
return False
return True
def can_multiply_by(self, other):
return self.can_multiply(self, other)
class Stream:
def extend(self, other):
# modify self using other
...
@classmethod
def from_file(cls, file):
...
@classmethod
def concatenate(cls, *streams):
s = cls()
for stream in streams:
s.extend(stream)
return s
@staticmethod
def total_bytes(*streams):
return sum(s.size for s in streams)
class Calendar:
def __init__(self):
self.events = []
def add_event(self, event):
self.events.append(event)
@classmethod
def from_json(cls, filename):
pass
@staticmethod
def is_weekend(dt):
pass
def staticmethod_example():
A.f1()
A.f2()
A().f1()
A().f2()
print(f'{A.f1=}')
print(f'{A.f2=}')
print(A().f1)
print(A().f2)
print(f'{type(A.f1)=}')
print(f'{type(A.f2)=}')
def classmethod_example():
A.g1()
A.g2()
A().g1()
A().g2()
print(f'{A.g1=}')
print(f'{A.g2=}')
print(f'{A().g1=}')
print(f'{A().g2=}')
print(f'{type(A.g1)=}')
print(f'{type(A.g2)=}')
def free_function_example():
h1()
h2()
print(f'{h1=}')
print(f'{h2=}')
print(f'{type(h1)=}')
print(f'{type(h2)=}')
def main():
staticmethod_example()
classmethod_example()
free_function_example()
if __name__ == '__main__':
main()