--- title: Drill 15 Solution tags: Drills-F20, 2020 --- # Drill 15 Solution ## Question 1 Some of these questions will refer to these dataclasses, representing bank users with (potentially shared) bank accounts. ``` @dataclass class Account: id : int balance : int @dataclass class Customer: name : str acct : Account ``` Imagine we create two customers with a shared bank account: ``` new_acct = Account(0, 250) cust1 = Customer("Giselle", new_acct) cust2 = Customer("Julia", new_acct) ``` If we then run ``` new_acct = Account(1, 175) ``` what would change in memory and the program directory? Select all that apply. [ ] No change to the directory [ ] No change to memory [ ] A new Account would be created in memory [ ] The existing Account corresponding to new_acct in memory would have its contents changed [ ] Maria and Jorge's account locations would change [ ] The directory entry for new_acct would change to map to a different memory location [ ] The original Account created for new_acct is removed from memory ::: spoiler Answer * A new Account would be created in memory * The directory entry for new_acct would change to map to a different memory location ::: ## Question 2 Assume we created several accounts using the following code: ``` for n in [1, 2, 3, 4]: Account(n, 0) # initialize an account with no balance ``` What code can we write to access the Account with id 2? ( ) We can't access any of the accounts -- the directory is empty ( ) loc 1001 ( ) Accounts[2] ( ) Not sure, but there must be a way to do this ::: spoiler Answer We can't access any of the accounts -- the directory is empty This code does not make any entries in the directory! It does create four Accounts in memory, but no names are ever associated with any of those locations. As a result, there is no way to get to any of these Accounts via code. ::: ## Question 3 Assume we created a list of accounts as follows: ``` new_acct = Account(2, 250) g_customer = Customer("Giselle", new_acct) j_customer = Customer("Julia", new_acct) new_acct = Account(3, 175) k_customer = Customer("Kathi", new_acct) all_customers = {"Kathi": k_customer, "Julia": j_customer, "Giselle": g_customer} ``` Which of the following expressions would extract the balance of 250 from the account with id 2? [ ] new_acct.balance [ ] j_customer.acct.balance [ ] all_customers.balance [ ] all_customers["Giselle"].balance [ ] k_customer.acct.balance ::: spoiler Answer * j_customer.acct.balance * all_customers["Giselle"].balance Sharing sets up multiple ways to reach the same piece of data in memory. While new_acct can access the 250 balance at first, once we reassign new_acct to the account with id 3, new_acct no longer reaches the account with id 2. However, that acct is still accessible through each of Julia and Giselle's accounts. It is also accessible through the dictionary. ::: ## Question 4 What will happen if we run this code? ``` @dataclass class City: name: str state: str chicago = City("Chicago", "IL") pizzas = {chicago: "deep dish", City("New York", "NY"): "actual pizza"} chicago.name = "CHICAGO" ``` ( ) The code will work correctly ( ) The code will throw an error because deep dish pizza is not real pizza ( ) The code will throw an error because dictionary keys need to be atomic values ( ) The code will throw an error because we are modifying a field on a frozen dataclass :::spoiler Answer The code will throw an error because dictionary keys need to be atomic values ::: ## Question 5 What will happen if we run this code? ``` @dataclass(frozen=True) class City: name: str state: str chicago = City("Chicago", "IL") pizzas = {chicago: "deep dish", City("New York", "NY"): "actual pizza"} chicago.name = "CHICAGO" ``` ( ) The code will work correctly ( ) The code will throw an error because deep dish pizza is not real pizza ( ) The code will throw an error because dictionary keys need to be atomic values ( ) The code will throw an error because we are modifying a field on a frozen dataclass :::spoiler Answer The code will throw an error because we are modifying a field on a frozen dataclass :::