Dec-10-2019, 03:57 PM
(This post was last modified: Dec-10-2019, 04:00 PM by Gribouillis.)
At first sight, there is no difference: if it doesn't find the
The
raise_amount
member in the instance, Python will look for the member in the class, then in the ancester classes if any.The
self.raise_amount
is the most robust syntax if you decide to change the structure of the classes later. For example if you change the name of the Employee
class, you won't need to change self.raise_amount
. Things get even better when you start defining subclasses, for exampleclass SpecialEmployee(Employee): raise_amount = 1.10 jane = SpecialEmployee('Jane', 'Doe', 5000) jane.apply_raise()Then Jane's new pay will be 5500 because
self.raise_amount
will use Jane's class instead of the Employee class.