-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursed_OOP.py
89 lines (81 loc) · 2.37 KB
/
cursed_OOP.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
"""
https://www.reddit.com/r/Python/comments/tceagd/better_oop_in_python_no_self_needed_anymore/
"""
# pylint:disable = missing-class-docstring,missing-function-docstring,invalid-name
# pylint:disable = attribute-defined-outside-init,no-method-argument
# mypy: ignore-errors
import types
class ClassNamespace(types.SimpleNamespace):
def __init__(self,superclass,name):
for k,v in superclass.__dict__.items():
self.__dict__.update({k:v})
self.class_name = name
def __repr__(self):
items = (f"{k}={v!r}" for k, v in self.__dict__['fields'].items())
itemstr = ",".join(items)
return f"Instance of {self.class_name} with fields {itemstr}"
def my_fields(self):
return self.__dict__['fields']
def empty():
empty_class = ClassNamespace(types.SimpleNamespace(),"Object")
empty_class.__dict__.update({'fields':{}})
return empty_class
def Counter(superclass=ClassNamespace.empty()):
obj = ClassNamespace(superclass,"Counter")
fields = obj.my_fields()
fields.update({'value':0})
def incrValue(qt=1):
nonlocal fields
fields['value'] += qt
def decrValue(qt=1):
nonlocal fields
fields['value'] -= qt
def conditionedIncr(qt=1):
nonlocal fields
if fields.get('flag',True):
incrValue(qt)
else:
decrValue(qt)
def getValue():
nonlocal fields
return fields['value']
obj.incrValue = incrValue
obj.decrValue = decrValue
obj.getValue = getValue
obj.conditionedIncr = conditionedIncr
return obj
def Flagger(superclass=ClassNamespace.empty()):
obj = ClassNamespace(superclass,"Flagger")
fields = obj.my_fields()
fields.update({'flag':False})
def toggle():
nonlocal fields
fields['flag'] = not fields['flag']
def getFlag():
nonlocal fields
return fields['flag']
obj.toggle = toggle
obj.getFlag = getFlag
return obj
def do_twice(func,*args):
func(*args)
func(*args)
c = Counter()
c.incrValue()
c.incrValue(3)
c2 = Counter()
do_twice(c2.decrValue,2)
print(c.getValue(),c2.getValue())
print(c)
print(c2)
f = Flagger(c)
print(f.getFlag(),f.getValue())
f.toggle()
print(f.getFlag(),f.getValue())
f.incrValue()
print(f.getFlag(),f.getValue())
f.conditionedIncr()
print(f.getFlag(),f.getValue())
print(f)
f2 = Flagger(f)
print(f2)