Class Setup: Circular References
Copyright (c) 2017 Kathi Fisler
In lecture, we are going to explore how to set up dataclasses to manage bank accounts. We’ll assume that each customer of the bank can have only one account (for simplicity), but that an account can be shared by multiple people (to make things interesting).
We’ll need dataclasses for each of Accounts and Customers to support this scenario.
@dataclass |
class Account: |
id: int |
balance: int |
owners: list # of Customer |
|
@dataclass |
class Customer: |
name: str |
acct: Account |
|
To prep for lecture, try to finish the following code example, which sets up a new account (id 15324) for a new customer (Kathi) with a balance of 150.
new_acct = Account(15324, 150, Customer("Kathi", __________)) |