Skip to content

Commit bfeb526

Browse files
committed
improve boolean
1 parent 566a3f1 commit bfeb526

1 file changed

Lines changed: 5 additions & 12 deletions

File tree

‎python/08-oop-basics.py‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ def __init__(self, title, pages):
179179
self.pages = pages
180180

181181
def is_short(self):
182-
if self.pages < 100:
183-
return True
182+
return self.pages < 100:
184183

185184
#What happens when you pass object to print?
186185
def __str__(self):
@@ -196,20 +195,14 @@ def __eq__(self, other):
196195

197196
#If should immutable, you could do something like this.
198197
#This replaces __hash__ = None
199-
def __hash__(self):
200-
return hash(self.title) ^ hash(self.pages) #xor with hash of attributes
201-
#from Mastering Object-Oriented Python
198+
# def __hash__(self):
199+
# return hash(self.title) ^ hash(self.pages) #xor with hash of attributes
200+
# #from Mastering Object-Oriented Python
202201

203202
book = Book("Are You My Mother", 72)
204-
print(book)
205203
equal_book = Book("Are You My Mother", 72)
206204
print("Are they considered equal?", book == equal_book) #yep
207205
print("Are they the same object?", book is equal_book) #nope
208206
book2 = Book("The Digging-est Dog", 72)
209207

210-
print(hash(book), hash(book2))
211-
212-
print("old hash", hash(book))
213-
book.title = "new"
214-
print("new hash", hash(book)) #BAD!!!
215-
#Hashes shouldn't change
208+
print(hash(book), hash(book2))

0 commit comments

Comments
 (0)