This repository was archived by the owner on Mar 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool.py
46 lines (35 loc) · 1.47 KB
/
bool.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
""" Introduction to the Python __bool__ method """
# An object of a custom class is associated with a boolean value.
# By default, it evaluates to True. For example
class Person:
def __init__(self, name):
self.name = name
print(bool(Person('Asad Hussain'))) # True
# To override this default behavior, you implement the __bool__ special method.
# The __bool__ method must return a boolean value, True or False.
class Person:
def __init__(self, age):
self.age = age
def __bool__(self):
return self.age > 18
print(bool(Person(20))) # True
print(bool(Person(17))) # False
""" The __len__ method """
# If a custom class doesn’t have the __bool__ method, Python will look for the __len__()
# method. If the __len__ is zero, the object is False. Otherwise, it’s True
# If a class doesn’t implement the __bool__ and __len__ methods, the objects of the
# class will evaluate to True
class Payroll:
def __init__(self, age):
self.age = age
def __len__(self):
return self.age
print(bool(Payroll))
print(bool(Payroll(0))) # False because __len__ becomes 0
""" Summary """
# All objects of custom classes return True by default.
# Implement the __bool__ method to override the default. The __bool__ method must
# return either True or False.
# If a class doesn’t implement the __bool__ method, Python will use the result of
# the __len__ method. If the class doesn’t implement both methods, the objects will
# be True by default.