Reputation: 1536
Docs->9.Classes->9.6 Private Variables
Following lines (line 3 until 5) make zero sense to me:
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.
If classname is the current class why does it say with leading underscores stripped? In the text everybody can see that the element _classname__spam
has an underscore in front of it.
EDIT
Somehow there should be class names with leading underscores like __Welcome but in the learning materials i have always fould examples like:
class NamingNow:
pass
...without underscores. However when i use the dir
function i get something like
>>> dir(Mapping)
['_Mapping__update', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'update']
Does this rule pertain to this __class__
thing?
Upvotes: 0
Views: 53
Reputation: 599788
It's saying that if the class name itself contains leading underscores, they will be stripped, but another underscore will always be prepended. So a class named _MyClass
would have attributes mangled to _MyClass_spam
, not __MyClass_spam
.
Upvotes: 3