ch06
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
# oop/operator.overloading.py
|
||||
class Weird:
|
||||
def __init__(self, s):
|
||||
self._s = s
|
||||
|
||||
def __len__(self):
|
||||
return len(self._s)
|
||||
|
||||
def __bool__(self):
|
||||
return '42' in self._s
|
||||
|
||||
|
||||
weird = Weird('Hello! I am 9 years old!')
|
||||
print(len(weird)) # 24
|
||||
print(bool(weird)) # False
|
||||
|
||||
weird2 = Weird('Hello! I am 42 years old!')
|
||||
print(len(weird2)) # 25
|
||||
print(bool(weird2)) # True
|
||||
|
||||
|
||||
"""
|
||||
$ python operator.overloading.py
|
||||
24
|
||||
False
|
||||
25
|
||||
True
|
||||
"""
|
||||
Reference in New Issue
Block a user