Skip to content

Instantly share code, notes, and snippets.

@matthiasgoergens
Created August 29, 2022 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthiasgoergens/6defaa0754fa4ffbee8770ae2de92a0e to your computer and use it in GitHub Desktop.
Save matthiasgoergens/6defaa0754fa4ffbee8770ae2de92a0e to your computer and use it in GitHub Desktop.
Booleans in Python
class A:
def __init__(self, x):
self.x = x
def __bool__(self):
print(f'bool: {self}')
return False
def __str__(self):
return f'A(x={self.x})'
print("Variant 1")
a = A(1) and A(2)
if a:
print("True")
print("Variant 2")
if A(3) and A(4):
print("True")
@matthiasgoergens
Copy link
Author

$ python3 bools.py 
Variant 1
bool: A(x=1)
bool: A(x=1)
Variant 2
bool: A(x=3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment