-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanything.py
97 lines (70 loc) · 1.66 KB
/
anything.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
"""
Defines the `Anything` and `Something` constants.
`Anything` compares true with any other object:
>>> Anything == 42
True
>>> 'hello' == Anything
True
You can use it to check that specific values in a data structure
have a value, but it doesn't matter what they are,
for example in a unit test:
>>> [1, 2, 3] == [1, Anything, 3]
True
>>> {'x': 10, 'y': -3} == {'x': 10, 'y': Anything}
True
>>> {'x': 10} == {'x': 10, 'y': Anything}
False
Inequality behaves consistently with equality:
>>> 'hello' != Anything
False
Even None is considered equal to Anything:
>>> Anything == None
True
If you want to make sure that a value is not None, use `Something`
instead:
>>> Something == None
False
>>> Something == 1
True
>>> 1 == Something
True
>>> Something != None
True
>>> Something != 'foo'
False
`Something` only checks for None specifically, i.e. it does not accept any
falsy value:
>>> Something == False
True
>>> Something != False
False
Equality between the two constants works as you probably expect:
>>> Anything == Anything
True
>>> Something == Something
True
>>> Anything == Something
True
>>> Something == Anything
True
"""
__all__ = ['Anything', 'Something']
class AnythingType(object):
def __eq__(self, other):
return True
def __ne__(self, other):
return False
def __repr__(self):
return 'Anything'
Anything = AnythingType()
class SomethingType(object):
def __eq__(self, other):
return other is not None
def __ne__(self, other):
return other is None
def __repr__(self):
return 'Something'
Something = SomethingType()
if __name__ == '__main__':
import doctest
doctest.testmod()