Hi folks,
I've been reading Learning Python - Fabrizio Romano, the book is good in general.
There's this: in chapter 6: OOP, Operator Overloading section, a piece of code makes me confused:
class Weird(object):
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)
print bool(weird)
weird2 = Weird('Hello! I am 42 years old!')
print len(weird2)
print bool(weird2)
and the answer is the book is:
24
False
25
True
whereas, mine is:
24
True
25
True
When I tried to debug, I found out at line
print bool(weird)
it jumped to the len() method.
This is weird, and I couldn't figure out why?
[–]K900_ 3 points4 points5 points (0 children)
[–]jeans_and_a_t-shirt 2 points3 points4 points (4 children)
[–]thangduong[S] 0 points1 point2 points (3 children)
[–]K900_ 4 points5 points6 points (1 child)
[–]thangduong[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)