GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python-patterns/null.py at master · yuxizhou/python-patterns

Expand file tree

/

null.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
82 lines (58 loc) · 1.56 KB
/

null.py

Copy path

File metadata and controls

82 lines (58 loc) · 1.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""http://code.activestate.com/recipes/68205-null-object-design-pattern/"""
class Null:
def __init__(self, *args, **kwargs):
"""Ignore parameters."""
return None
def __call__(self, *args, **kwargs):
"""Ignore method calls."""
return self
def __getattr__(self, mname):
"""Ignore attribute requests."""
return self
def __setattr__(self, name, value):
"""Ignore attribute setting."""
return self
def __delattr__(self, name):
"""Ignore deleting attributes."""
return self
def __repr__(self):
"""Return a string representation."""
return "<Null>"
def __str__(self):
"""Convert to a string and return it."""
return "Null"
def test():
"""Perform some decent tests, or rather: demos."""
# constructing and calling
n = Null()
print(n)
n = Null('value')
print(n)
n = Null('value', param='value')
print(n)
n()
n('value')
n('value', param='value')
print(n)
# attribute handling
n.attr1
print('attr1', n.attr1)
n.attr1.attr2
n.method1()
n.method1().method2()
n.method('value')
n.method(param='value')
n.method('value', param='value')
n.attr1.method1()
n.method1().attr1
n.attr1 = 'value'
n.attr1.attr2 = 'value'
del n.attr1
del n.attr1.attr2.attr3
# representation and conversion to a string
assert repr(n) == '<Null>'
assert str(n) == 'Null'
if __name__ == '__main__':
test()
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.