class Person: # __init__ method is called after a new Person # object is created. def __init__(self, the_name, the_gender, the_age): self.name = the_name self.gender = the_gender self.age = the_age # Regular method. def have_birthday(self): self.age += 1 # __str__ method is called by the # standalone str method. def __str__(self): return_value = self.name + ';' + \ self.gender ';' + str(self.age) return return_valueAnswer the Zoom Poll 7 questions based on the preceding Person class. Correct answers are marked with *.
for i in range(0, 5): print(i, end=" ")a. 0 1 2 3 4 b. 0 1 2 3 4 5
For i in the range from 1 to 10 If frames[i][0] equals 10 and frames[i+1][0] equals 10 assign 10 to next_ball_1. End if. End if. Ans: Translation of pseudocode into Python:
for i in range(1, 11): if frames[i][0] == 10 and frames[i+1][0] == 10: next_ball_1 = 10
# === pet.py module ========================== 1 Class Pet 2 3 # Initialize instance variables. 4 _vaccinated is a private instance variable, 5 which means that it should not be used 6 outside of the Pet class 7 def _init__(self, the_name, the_animal_type): 8 name = the_name 9 the_animal_type = self.animal_type 10 self._vaccinated = False 11 12 # Return Yes if pet is vaccinated, 13 # No, otherwise. 14 def is_vaccinated( ): 15 if self._vaccinated: 16 return "Yes" 17 else 18 return No 19 20 # Vaccinate pet. 21 def vaccinate(self): 22 self.vaccinated = true 23 24 # Show pet as a string. 25 def str(self): 26 ret_val = f"Name: (self.name)\n" + \ 27 "Animal Type: {self.animal_type}\n" + / 28 f"Is Vaccinated: {self.is_vaccinated( )}\n" 29 return retval # === test1.py module ======================== 1 from pet imports Pet 2 pet1 = Pet("Sparky", "Dog") 3 pet2 = Pet("Clair" "Cat") 4 print(pet1) 5 print(pet 2) 6 print(pet1.name, pet2.name) 7 print(pet1.animal_type, pet2.animal_type) 8 print(pet1.is_vaccinated( ), \ 9 pet2.is_vaccinated( )) 10 print( ) 11 vaccinate( ) 12 pet2.vaccinate 13 print(pet1) 14 print pet2 # ============================================Ans: See the Pet Example.
instance variable instance method dunder method local variable constructor parameter argument public instance variable public method private instance variable private methodAns:
instance variable: name animal_type _vaccinated instance method: __init__ is_vaccinated vaccinate __str__ dunder method: __init__ __str__ local variable: ret_val constructor: Pet parameter: self the_name the_animal_type argument: None public instance variable: name animal_type public instance method: is_vaccinated vaccinate private instance variable: _vaccinated private instance method: __init__ __str__
2 3 4 5 6 7 8 9 10 11 (J) 12 (Q) 13 (K) 14 (A)The possible values of suit are
"C" "D" "H" "S"
This section will be discussed on Monday, Oct 7.
s = 'apple' print(str(s), repr(s))
apple 'apple'apple is the user friendly representation; 'apple' is the technical representation.
=== Module: person.py ====================== class Person: def __init__(self, name, gender, age): self.name = name self.gender = gender self.age = age === Module: test1.py ======================= from person import Person p = Person("Bill", "M", 23) print(str(p)) print(repr(p)) === Output: ================================ <person.Person object at 0x032C1118> <person.Person object at 0x032C1118>
def __str__(self): return f"{self.name};{self.gender};{self.age}" # Output: Alice;F;11 <person.Person object at 0x034B1118>
def __repr__(self): return f"<{self.name};{self.gender};{self.age}>" # Output: Alice;F;11 <Alice;F;11>
def __repr__(self): return f"<{str(self)}>"
def __str__(self): return str(self)
This section will be discussed on Monday, Oct 7.
from card import Card c1 = Card(12, 'H') # Rank of Queen is 12 c2 = Card(14, 'H') # Rank of Ace is 14 c3 = Card(11, 'C') # Rank of Jack is 11 c4 = Card(11, 'H') c5 = Card(11, 'S') poker_hand = [c1, c2, c3, c4, c5] print(poker_hand)This is the output:
[<card.Card object at 0x038440B0>, <card.Card object at 0x03AA6950>, <card.Card object at 0x03AA6970>, <card.Card object at 0x03AA6910>, <card.Card object at 0x03CCBF30>]This is perplexing. What does it mean? It turns out that the default representations of the 5 card objects are shown as returned by the Card repr method. In fact, if we use the repr method to return the technical representation of c2, which is the ace of hearts, we have
print(repr(c2)) # Output: <card.Card object at 0x03AA6950>This is the object from the Card class in the card module that is stored at the computer memory location 03AA6950 hex, which is not very useful for us. To get a more useful representation of the cards in the list, we need to define our own custom __repr__ method, which will be called in the print statement
print(repr(c2))
def __repr__(self): return '<' + str(self) + '>'and run the print statement again:
print(repr(c2)) # Output: <AH>
from card import Card c1 = Card(12, 'H') # Rank of Queen is 12 c2 = Card(14, 'H') # Rank of Ace is 14 c3 = Card(11, 'C') # Rank of Jack is 11 c4 = Card(11, 'H') c5 = Card(11, 'S') poker_hand = [c1, c2, c3, c4, c5] print(poker_hand) # Output: [<QH>, <AH>, <JC>, <JH>, <JS>]Now we get more useful output when printing the list because our custom __repr__ is used when printing the list.