forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra_bool_eval.py
More file actions
33 lines (25 loc) · 903 Bytes
/
extra_bool_eval.py
File metadata and controls
33 lines (25 loc) · 903 Bytes
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
# Python carefully avoids evaluating bools more than once in a variety of situations.
# Eg:
# In the statement
# if a or b:
# it doesn't simply compute (a or b) and then evaluate the result to decide whether to
# jump. If a is true it jumps directly to the body of the if statement.
# We can confirm that this behaviour is correct in python code.
# A Bool that raises an exception if evaluated twice!
class ExplodingBool():
def __init__(self, value):
self.value = value
self.booled = False
def __bool__(self):
assert not self.booled
self.booled = True
return self.value
y = (ExplodingBool(False) and False and True and False)
print(y)
if (ExplodingBool(True) or False or True or False):
pass
assert ExplodingBool(True) or False
while ExplodingBool(False) and False:
pass
# if ExplodingBool(False) and False and True and False:
# pass