May 2019
Beginner
528 pages
29h 51m
English
Class Account’s methods validate their arguments to ensure that the balance is always valid—that is, always greater than or equal to 0.00. In the previous example, we used the attributes name and balance only to get the values of those attributes. It turns out that we also can use those attributes to modify their values. Consider the Account object in the following IPython session:
In [1]: from account import AccountIn [2]: from decimal import DecimalIn [3]: account1 = Account('John Green', Decimal('50.00'))In [4]: account1.balanceOut[4]: Decimal('50.00')
Initially, account1 contains a valid balance. Now, let’s set the balance attribute to an invalid negative value, then display the balance:
In [5]: ...Read now
Unlock full access