This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: @staticmethod mangles name of outside class with __ (it is not inside class with staticmethod, but name mangled anyway), so NameError is produced
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Leeland Morgan, rhettinger, xtreak
Priority: normal Keywords:

Created on 2018-07-23 10:15 by Leeland Morgan, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg322188 - (view) Author: Leeland Morgan (Leeland Morgan) Date: 2018-07-23 10:15
Hi! I'm not sure this is bug, but I did not find info about outside classes name mangling.

Here is a repro url:
msg322299 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-07-24 13:13
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. (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 :

msg322311 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-07-24 15:44
Name mangling is intended to create class local references (that is why the class name is prepended) and can be used to add a layer of privacy to calls *within* a class.   It is not intended to do what you're trying to do outside of class definitions.
Date User Action Args
2022-04-11 14:59:03adminsetgithub: 78377
2018-07-24 15:44:24rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg322311

resolution: not a bug
stage: resolved
2018-07-24 13:13:19xtreaksetnosy: + xtreak
messages: + msg322299
2018-07-23 10:15:08Leeland Morgancreate