This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
null_object.py
93 lines (66 loc) · 1.72 KB
/
null_object.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
"""Null Object pattern
"""
import random
class RealObject(object):
def __init__(self, name):
self.name = name
def __call__(self, *args, **kwargs):
return print("Called with args {} and kwargs {}".format(args, kwargs))
def is_null(self):
return False
def do_stuff(self):
print("do some real stuff")
def get_stuff(self):
return "some real stuff"
class NullObject(RealObject):
def __init__(self, *args, **kwargs):
pass
def __call__(self, *args, **kwargs):
return self
def __repr__(self):
return "<Null>"
def __str__(self):
return "Null"
def __getattr__(self, attr_name):
return self
def __setattr__(self, attr_name, attr_value):
return self
def __delattr__(self, attr_name):
return self
def is_null(self):
return True
def do_stuff(self):
pass
def get_stuff(self):
return None
def give_me_an_object(name):
num = random.random()
cls = RealObject if num > 0.5 else NullObject
print("Class: {}".format(cls.__name__))
print("__init__")
return cls(name)
def main():
name = "Bob"
# instatiation and representation
obj = give_me_an_object(name)
print("__str__")
print(obj)
print(repr(obj))
# attribute handling
print("__getattr__ ")
print(obj.name)
print("__setattr__")
obj.name = "John"
print(obj.name)
print("__delattr__")
del obj.name
# object calling
print("__call__")
obj("hello", 123, some_key=456)
# methods for this particular example
print("do_stuff")
obj.do_stuff()
my_stuff = obj.get_stuff()
print(my_stuff)
if __name__ == "__main__":
main()