Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101370

Unable to access module attribute with underscores in class method, Python 3

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Joseph Fox-Rabinovitz <jfoxrabinovitz@gmail.com>
Newsgroups comp.lang.python
Subject Unable to access module attribute with underscores in class method, Python 3
Date Thu, 7 Jan 2016 11:14:10 -0500
Lines 38
Message-ID <mailman.62.1452258624.2305.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de FzaYztUSJrtRsZkAHpdIMQJE8lNtICtDbjUfFs3nP3ww==
Return-Path <jfoxrabinovitz@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.05; 'overflow': 0.07; '__init__': 0.09; 'expectation': 0.09; 'nameerror:': 0.09; 'subject:method': 0.09; 'subject:module': 0.09; 'python': 0.10; 'stack': 0.13; 'def': 0.13; '(within': 0.16; 'b()': 0.16; 'illustrates': 0.16; 'nameerror': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:access': 0.16; 'subject:class': 0.16; 'attribute': 0.18; 'module,': 0.18; 'fix': 0.21; 'posted': 0.21; '"",': 0.22; 'defined': 0.23; 'bit': 0.23; '(most': 0.24; 'module': 0.25; 'question': 0.27; 'message- id:@mail.gmail.com': 0.27; 'container': 0.29; 'convince': 0.29; 'dictionary': 0.29; 'starts': 0.29; 'that.': 0.30; 'creating': 0.30; 'code': 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'reference,': 0.33; 'traceback': 0.33; 'wrap': 0.33; 'similar': 0.33; 'file': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'skip:& 10': 0.37; 'method': 0.37; 'doing': 0.38; 'received:209': 0.38; 'hi,': 0.38; 'does': 0.39; 'unable': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'subject:with': 0.40; 'some': 0.40; 'skip:n 10': 0.62; 'here:': 0.63; 'within': 0.64; 'p.s.': 0.65; 'results': 0.66; 'difference.': 0.84; 'forbidden.': 0.84; 'received:209.85.215.42': 0.84; 'subject:, \n ': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=kT3uZNVilyx/IZHK8kJ0/EsvnvxjbbyxyuD30rtYL10=; b=ns5U6/QWOf+FnuhRahY5ieY3Re3lpXmiFfDoMsP1X+yiJeoiXcFauoyPKGDf9tVn65 f863p5hqNH0C9eLs7MgGOGWqUvZvG6M99AuzjM1IBTQYW7GZEN/9vp/PBImkdCXQYjoX AxX0qQlUwQdPuY1IXhoGRAO2ruxaNp1pZg1xGPcVeTjAq6bPVTBZMYu/bIvTzSJBC9o0 VB0o4sj4kESaT/EgrLAzXGnnWyc4C5TWv+wxILCseP+bmNy3XMlNS0C3F7J2zfYH5WYs +uJw9pNspnEMOeKdw8lCAbaDsFsLztTfAQkLmBlgRjKSMjPXbWjeNzO+rz9Q9tr657iS yibQ==
X-Received by 10.25.24.156 with SMTP id 28mr26729005lfy.27.1452183250534; Thu, 07 Jan 2016 08:14:10 -0800 (PST)
X-Mailman-Approved-At Fri, 08 Jan 2016 08:10:23 -0500
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101370

Show key headers only | View raw


Hi,

I have a module attribute whose name starts with a pair of underscores. I
am apparently unable to access it directly in a class method (within the
same module, but that is not relevant as far as I can tell). The following
bit of code illustrates the situation:

__a = 3
class B:
    def __init__(self):
        global __a
        self.a = __a
b = B()

This results in a NameError because of name-mangling, despite the global
declaration:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __init__
NameError: name '_B__a' is not defined

Not using global does not make a difference. I posted a similar question on
Stack Overflow, where the only reasonable answer given was to wrap __a in a
container whose name is not mangled. For example, doing `self.a =
globals()['__a']` or manually creating a dictionary with a non-mangled name
and accessing that.

I feel that there should be some way of accessing __a within the class
directly in Python 3. Clearly my expectation that global would fix the
issue is incorrect. I would appreciate either a solution or an explanation
of what is going on that would convince me that accessing a module
attribute in such a way should be forbidden.

    -Joseph Fox-Rabinovitz

P.S. For reference, the Stack Overflow question is here:
http://stackoverflow.com/questions/34621484/how-to-access-private-variable-of-python-module-from-class

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Unable to access module attribute with underscores in class method, Python 3 Joseph Fox-Rabinovitz <jfoxrabinovitz@gmail.com> - 2016-01-07 11:14 -0500
  Re: Unable to access module attribute with underscores in class method, Python 3 Steven D'Aprano <steve@pearwood.info> - 2016-01-09 03:07 +1100
    Re: Unable to access module attribute with underscores in class method, Python 3 Chris Angelico <rosuav@gmail.com> - 2016-01-09 03:16 +1100
      Re: Unable to access module attribute with underscores in class method, Python 3 Steven D'Aprano <steve@pearwood.info> - 2016-01-09 12:25 +1100

csiph-web