Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Joseph Fox-Rabinovitz 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: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de FzaYztUSJrtRsZkAHpdIMQJE8lNtICtDbjUfFs3nP3ww== Return-Path: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101370 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 "", line 1, in File "", 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