-
Notifications
You must be signed in to change notification settings - Fork 206
/
chained_comparisons.py
79 lines (54 loc) · 1.74 KB
/
chained_comparisons.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
def chained_comparisons_good_uses(x, y, z):
if 0 < x < 1:
print("x in range (0,1)")
if 0 <= x < 1:
print("x in range [0,1)")
if x <= y <= z:
print("y in range [x,z]")
if x >= y >= 1:
print("y in range [1,x]")
if x == y == z:
print("x,y,z all equal")
def chained_comparisons_ok_to_questionable_uses(x, y, z):
if 0 < x < y == z < 1:
print("x,y in (0,1) with x<y and z==y")
if x <= y <= z != 1:
print("y in range(x,z) with z != 1")
if x == y == z != 1:
print("x,y,z all equal something that isn't 1")
if x is y is z:
print("x,y,z all identical")
if x is y is z in [1, 2, 3]:
print("x,y,z all identical and in [1,2,3]")
def chained_comparisons_bad_uses(x, y, z):
if x < y > z:
print("y > max(x,z)")
if x != y != z:
print("kinda looks like x,y,z all distinct, but may have x==z")
if 0 > x < y != z > 1:
print("WHY??")
if 0 < x > 1 >> y << 1 < z > 1:
print("please remove this from the language")
def maybe_someday():
import numpy as np
x = np.arange(10)
y = x[3 < x < 5] # not today
y = x[(3 < x) & (x < 5)]
def rules_of_thumb(x, y, z):
pass
# my personal rules of thumb for chained comparisons
# feel free to use <, <=, and == together
# feel free to use >, >=, and == together
# do not use <, <= with >, >=
# feel free to use multiple == in a row
# do not use != anywhere in a chain
# do not use "is" or "in" in a chain
def main():
x, y, z = 0, 1, 2
if x < y > z:
print(True)
else:
print(False)
comparisons = ['<', '>', '<=', '>=', '==', '!=', 'is', 'is not', 'in', 'not in']
if __name__ == '__main__':
main()