Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1def get_name(self): return self.__name | AUsing brackets on a @property. A property is read like an attribute, so it should be pet.name. | |
| 2@property | Bstudent.name = 'Sam' # AttributeError | |
| 3Outside code reads a private attribute by calling a getter method | CA decorator that lets a getter method be read like an attribute, with no brackets | |
| 4A property with no setter is read-only, so assigning to it causes an error | Dprint(account.get_balance()) | |
| 5print(pet.__name) | EAccessing the private attribute directly from outside the class. Call a getter such as pet.get_name() instead. | |
| 6print(pet.name()) | FA getter method that returns the private attribute __name so outside code can read it |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
@property — A decorator above a getter method. It lets outside code read a private attribute like a normal attribute, such as obj.name, with no brackets. With no setter, the attribute is read-only.class Student:
def get_name(self):
return self.__name@property
def name(self):
return self.__nameacct = Account(50)
print(acct.balance)
# no brackets needed@property
def balance(self):
return self.__balancetry:
acct.balance = 500
except AttributeError: print("Read-only")