[New-bugs-announce] [issue26044] Name mangling overrides externally defined names

Joseph Fox-Rabinovitz report at bugs.python.org
Thu Jan 7 14:28:19 EST 2016


New submission from Joseph Fox-Rabinovitz:

I will begin by including three code snippets that do not work to illustrate the issue. All snippets were run as python modules/scripts from the command line using "python snippet.py":

**1**

__a = 3
print(locals())
class T:
    def __init__(self):
        global __a
        self.a = __a
t1 = T()

**2**

__a = 3
print(locals())
class T:
    def __init__(self):
        self.a = __a
t2 = T()

**3**

__a = 3
print(locals())
class T:
    def __init__(self):
        m = sys.modules[__name__]
        self.a = m.__a
t3 = T()

All three snippets fail in the line assigning `self.a`. The first two produce `NameError: name '_T__a' is not defined`. The third produces `AttributeError: module '__main__' has no attribute '_T__a'`. The problem in all three cases is that name mangling supercedes any other operation to the point that it mangles elements that are explicitly stated not to belong to the class. This behavior is not intuitive or expected.

I am running `Python 3.5.1 :: Continuum Analytics, Inc.` (using the Anaconda platform) on a Red Hat 6.5 machine. I have tried the same thing on Arch Linux (also with Python 3.5.1 and anaconda) with identical results.

----------
components: Interpreter Core
messages: 257714
nosy: madphysicist
priority: normal
severity: normal
status: open
title: Name mangling overrides externally defined names
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26044>
_______________________________________


More information about the New-bugs-announce mailing list