Name mangling

You'll notice that some of the variable names have leading double underscores. This is to define them as specifically related to this class. This is referred to as name mangling in Python.

In Python, a single, leading underscore creates a so-called private attribute or method, designed to keep the attribute from being directly accessed. It's not really private, as it can be called explicitly, but it won't be automatically imported when using the import statement.

Name mangling causes the attribute with double underscores to become _ClassName__attribute internally within Python. That is, the class name the attribute is associated with becomes part of the fully qualified name for the attribute.

Name mangling is intended to provide private instance variables and methods to classes without the programmer having to worry about name shadowing or other attributes with the exact name.

While not necessary, using name mangling for property variables is an easy way to ensure properties are returned or set for the desired object, without having to worry about capturing the wrong one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset