...
/Replacing Setters and Getters with a Python property
Replacing Setters and Getters with a Python property
We'll cover the following...
Let’s pretend that we have some legacy code that someone wrote who didn’t understand Python very well. If you’re like me, you’ve already seen this kind of code before:
Press + to interact
from decimal import Decimalclass Fees(object):""""""def __init__(self):"""Constructor"""self._fee = Nonedef get_fee(self):"""Return the current fee"""return self._feedef set_fee(self, value):"""Set the fee"""if isinstance(value, str):self._fee = Decimal(value)elif isinstance(value, Decimal):self._fee = value
To use this class, we have to use the setters and getters that are defined:
Press + to interact
f = Fees()f.set_fee("1")value = f.get_fee()print(value)
If you want to add the normal dot notation access of attributes to ...
Access this course and 1400+ top-rated courses and projects.