diff --git a/beginner_python/06-loop-basics.py b/beginner_python/06-loop-basics.py index 643d310..b46c060 100644 --- a/beginner_python/06-loop-basics.py +++ b/beginner_python/06-loop-basics.py @@ -5,7 +5,7 @@ for x in languages: print(x) -#The body of this loop (the indented part) will run 4 times +#The body of this loop (the indented part) will run 3 times #Each time is known as an iteration #Each iteration, the variable x will contain one element #iteration 1 will be the first element diff --git a/python/08-oop-basics.py b/python/08-oop-basics.py index eb1956d..cd7ce31 100644 --- a/python/08-oop-basics.py +++ b/python/08-oop-basics.py @@ -87,7 +87,7 @@ def log(self): def is_short(self): if self.pages < 100: - return true + return True book = Book("Are You My Mother?", 72) book.log() #no need to pass anything to self parameter. @@ -110,7 +110,7 @@ def is_short(self): def book_is_short(book): if book.pages < 100: - return true + return True if book_is_short(book): #do something @@ -151,7 +151,7 @@ def log(self): def is_short(self): if self.pages < 100: - return true + return True book = Book("Are You My Mother?", 72) @@ -179,8 +179,7 @@ def __init__(self, title, pages): self.pages = pages def is_short(self): - if self.pages < 100: - return true + return self.pages < 100 #What happens when you pass object to print? def __str__(self): @@ -188,8 +187,7 @@ def __str__(self): #What happens when you use ==? def __eq__(self, other): - if(self.title == other.title and self.pages == other.pages): - return True + return self.title == other.title and self.pages == other.pages #It's approriate to give something for __hash__ when you override __eq__ # #This is the recommended way if mutable (like it is here): @@ -197,20 +195,14 @@ def __eq__(self, other): #If should immutable, you could do something like this. #This replaces __hash__ = None - def __hash__(self): - return hash(self.title) ^ hash(self.pages) #xor with hash of attributes - #from Mastering Object-Oriented Python + # def __hash__(self): + # return hash(self.title) ^ hash(self.pages) #xor with hash of attributes + # #from Mastering Object-Oriented Python book = Book("Are You My Mother", 72) -print(book) equal_book = Book("Are You My Mother", 72) print("Are they considered equal?", book == equal_book) #yep print("Are they the same object?", book is equal_book) #nope book2 = Book("The Digging-est Dog", 72) -print(hash(book), hash(book2)) - -print("old hash", hash(book)) -book.title = "new" -print("new hash", hash(book)) #BAD!!! -#Hashes shouldn't change \ No newline at end of file +print(hash(book), hash(book2)) \ No newline at end of file diff --git a/python/09-objects-io.py b/python/09-objects-io.py index 029fc80..dbab3b6 100644 --- a/python/09-objects-io.py +++ b/python/09-objects-io.py @@ -6,8 +6,7 @@ def __init__(self, title, pages): self.pages = pages def is_short(self): - if self.pages < 100: - return true + return self.pages < 100 #What happens when you pass object to print? def __str__(self): @@ -15,8 +14,7 @@ def __str__(self): #What happens when you use ==? def __eq__(self, other): - if(self.title == other.title and self.pages == other.pages): - return True + return self.title == other.title and self.pages == other.pages #It's approriate to give something for __hash__ when you override __eq__ # #This is the recommended way if mutable (like it is here): diff --git a/python/book.py b/python/book.py index 6df242a..0e89f6e 100644 --- a/python/book.py +++ b/python/book.py @@ -1,25 +1,20 @@ class Book(): - favs = [] #class + favs = [] def __init__(self, title, pages): self.title = title self.pages = pages def is_short(self): - if self.pages < 100: - return true + return self.pages < 100 - #What happens when you pass object to print? def __str__(self): return f"{self.title}, {self.pages} pages long" - #What happens when you use ==? def __eq__(self, other): - if(self.title == other.title and self.pages == other.pages): - return True + return self.title == other.title and self.pages == other.pages + - #It's approriate to give something for __hash__ when you override __eq__ - # #This is the recommended way if mutable (like it is here): __hash__ = None def __repr__(self): #added to make list of items invoke str