as5
as5
Instructions
From the typing library, import Dict and List.
Using a class imported from typing, add type hints to the roster dictionary to
include details about the types of keys and values.
Update the creation of the agents list to add type hints for both the object
itself, as well as the type of the elements it stores.
# Unpack the values and add type hints for the new list
agents: ____[____] = [
f"Agent {agent}, {missions} missions" \
for agent, missions in roster.items()
]
--------------------------------------------------
Exercise-2
Type hinting with custom classes
In this exercise, you'll use built-in type keywords, the typing library, and your
own custom class to practice type hinting. List has been imported from the typing
library. Time to get started!
Instructions 1/3
Create an Agent class that takes the parameters codename (a string), and missions
(an integer); add type hints to both the constructor definition, and the method
itself.
# Define an agent class with a constructor, add type hints
class Agent:
def __init__(self, ____: ____, ____: ____):
self.____: ____ = codename
self.____: int = ____
------------------------------------------------------------------
Instructions 2/3
Define an add_mission() method that takes a single parameter, location (a string),
and returns no value; make sure to add type hints to the definition of the method.
class Agent:
def __init__(self, codename: str, missions: int):
self.codename: str = codename
self.missions: int = missions
-----------------------------------------------------
Instructions 3/3
Add type hints to the definition of an Agent object, called chuck.
Create and type hint a list of locations (all strings), and use the chuck object to
add a mission for each of these locations.
class Agent:
def __init__(self, codename: str, missions: int):
self.codename: str = codename
self.missions: int = missions
--------------------------------------------------------------
CLASS WORK
Exercise-1
Accessing the balance attribute
In this exercise, you'll practice accessing the balance attribute of a BankAccount
class
that's implemented a descriptor using the @property decorator.
The BankAccount class has been created for you, as shown below:
class BankAccount:
def __init__(self, balance):
self.balance = balance
@property
def balance(self):
return f"${round(self._balance, 2)}"
@balance.setter
def balance(self, new_balance):
if new_balance > 0:
self._balance = new_balance
@balance.deleter
def balance(self):
print("Deleting the 'balance' attribute")
del self._balance
Instructions
Output the balance attribute of the newly-created checking_account object using
print().
Set the value of balance to 150 and again output the updated attribute.
Delete the balance attribute from the checking_account object.
-----CODE----
checking_account = BankAccount(100)
-----------------------------------------------------------
Exercise-2
Using the @property decorator
Now that you've practiced working with a class that implements descriptors, you'll
get to build your own using the @property decorator. Good luck!
Instructions 1/3
Use the @property decorator to begin the process of creating a descriptor for the
email attribute; this method will return the email address inside of an f-string.
----CODE----
class BankAccount:
def __init__(self, email):
self.email = email
------------------------------------------------
Instructions 2/3
Create and decorate a method in the BankAccount class to set the value of the email
attribute, if the new email address contains the @ symbol
---CODE---
class BankAccount:
def __init__(self, email):
self.email = email
@property
def email(self):
return f"Email for this account is: {self._email}"
-----------------------------------------------------------------------
Instructions 3/3
Define and decorate a method that will be used when the email attribute is deleted
from a BankAccount object.
----CODE---
class BankAccount:
def __init__(self, email):
self.email = email
@property
def email(self):
return f"Email for this account is: {self._email}"
@email.setter
def email(self, new_email_address):
if "@" in new_email_address:
self._email = new_email_address
else:
print("Please make sure to enter a valid email.")
-----------------------------------------------------------------