[issue34196] @staticmethod mangles name of outside class with __ (it is not inside class with staticmethod, but name mangled anyway), so NameError is produced

Karthikeyan Singaravelan report at bugs.python.org
Tue Jul 24 09:13:19 EDT 2018


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

I think below doc gives some indication that it always occurs within the definition of the class (hence inside a static method) irrespective of the syntactic position for any valid identifier and hence for class __SuperPrivateClass too. I will let others explain on this better or give more information.

https://docs.python.org/3/tutorial/classes.html#private-variables (second paragraph)

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.

SO answer that gave me the link : https://stackoverflow.com/a/1301369/2610955

Thanks

----------
nosy: +xtreak

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34196>
_______________________________________


More information about the Python-bugs-list mailing list