Issue 34196: @staticmethod mangles name of outside class with __ (it is not inside class with staticmethod, but name mangled anyway), so NameError is produced (original) (raw)
Issue34196
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: https://repl.it/@LeelandMorgan/FlatSociableResource | ||
msg322299 - (view) | Author: Karthikeyan Singaravelan (xtreak) * ![]() |
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. 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 | ||
msg322311 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
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. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:59:03 | admin | set | github: 78377 |
2018-07-24 15:44:24 | rhettinger | set | status: open -> closednosy: + rhettingermessages: + resolution: not a bugstage: resolved |
2018-07-24 13:13:19 | xtreak | set | nosy: + xtreakmessages: + |
2018-07-23 10:15:08 | Leeland Morgan | create |