Thursday 12 July 2018

Python Object Attribute Variable Names Beginning With Double Underscores

Cited from the book "Python How to Program"

When Python encounters an attribute name that begins with two underscores, the interpreter performs name mangling on the attribute, to prevent indiscriminate access to the data. Name mangling changes the name of an attribute by including information about the class to which the attribute belongs. For example, if the Time constructor contained the line self.__hour = 0. Python creates an attribute called _Time__hour, instead of an attribute called __hour.

No comments:

Post a Comment