Advanced Lesson 2
Advanced Object-Oriented Programming
Chapter 3: Encapsulation
Encapsulation in inheritance
When you are designing your class so that it can be subclassed, you should be aware that the non-public attributes will not be accessible by your subclass. For example, self.__name
cannot be directly accessed by Hero
(because it has been automatically renamed to _Character__name
).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Python’s recommendation is for you to prefix the attribute name with a single underscore instead. Technically, it will not ‘hide’ the attribute as before, but it hints to a programmer that the attribute should only be used by a subclass. Of course, this also means that anybody can access this attribute freely, but Python trusts that a programmer will do the right thing and not misuse the given freedom!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
If you have previously done OOP with Java or C++, you can think of this as marking the attribute as “protected” (without any strict enforcement).