Name Mangling in Python
A Python programmer can explicitly designate that the name of an attribute within a class body should be mangled by using a name with two leading underscores and not more than one trailing underscore. For example, __thing
will be mangled, as will ___thing
and __thing_
, but __thing__
and __thing___
will not.
On encountering name mangled attributes, Python transforms these names by a single underscore and the name of the enclosing class, for example:
class Test(object): def __mangled_name(self): pass def normal_name(self): pass print dir(Test)will output:
['_Test__mangled_name', '__doc__', '__module__', 'normal_name']Read more about this topic: Name Mangling
Famous quotes containing the word mangling:
“How can anyone be interested in war?that glorious pursuit of annihilation with its ceremonious bellowings and trumpetings over the mangling of human bones and muscles and organs and eyes, its inconceivable agonies which could have been prevented by a few well- chosen, reasonable words. How, why, did this unnecessary business begin? Why does anyone want to read about itthis redundant human madness which men accept as inevitable?”
—Margaret Anderson (18861973)