Path: csiph.com!feeder.erje.net!2.us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Joseph Fox-Rabinovitz Newsgroups: comp.lang.python Subject: Re: Unable to access module attribute with underscores in class method, Python 3 Date: Thu, 7 Jan 2016 12:33:25 -0500 Lines: 57 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de QHqzi3bI0ZlB1tpMB3OFKwAN2CPVJUQfzrBj7xsAuRlg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'overflow': 0.07; 'raises': 0.07; '__init__': 0.09; 'expectation': 0.09; 'nameerror:': 0.09; 'subject:method': 0.09; 'subject:module': 0.09; 'python': 0.10; 'jan': 0.11; 'exception': 0.13; 'stack': 0.13; 'def': 0.13; 'subject: \n ': 0.15; 'thu,': 0.15; '(within': 0.16; '2016': 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; 'wrote:': 0.16; 'attribute': 0.18; 'module,': 0.18; 'fix': 0.21; 'posted': 0.21; '"",': 0.22; 'am,': 0.23; 'defined': 0.23; 'bit': 0.23; 'tried': 0.24; '(most': 0.24; 'header:In-Reply-To:1': 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; 'getting': 0.33; 'point': 0.33; 'class': 0.33; 'reference,': 0.33; 'traceback': 0.33; 'wrap': 0.33; 'similar': 0.33; 'file': 0.34; '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; 'subject:: ': 0.37; 'method': 0.37; 'doing': 0.38; 'detail': 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; 'more': 0.63; 'here:': 0.63; 'within': 0.64; 'p.s.': 0.65; 'results': 0.66; 'difference.': 0.84; 'forbidden.': 0.84; 'greedy': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=dF3R6uW8SP/2N0Lurotr5Ja3fZQjXUiACfvGb9KKmSw=; b=rqbkNHmKtr1oNG94WYProNKlSUgXx1LJX2NL7XYKvbZLia/Tld6YfSg5s4B+VTvEc7 O1gF5dBZPVxjdzqFPV5tTaYG/3CpBk9bmxACAkxw6zTDAW1Pp0xL/6vhOpSuJMYbs9sI 1Kfo81UgfQy94qEufLLaa1L7NlTq4z8hLkr+0wNoFUfmAwIKxtGto+ltf53EbB6Q7ux7 k4gJ9GmylBPPk4gp/chGN+foACkFjPVI4W6CAcKIA0oLg2BSFIKC+3129mxJpMptNTm1 UrAipT3472G8VFlq0b9w9Go1uo1+pnxJvdJnCVsVMsx0HASULLF3ibXAx4bAX4aw6Qjt 3XFA== X-Received: by 10.112.14.39 with SMTP id m7mr33581148lbc.20.1452188005665; Thu, 07 Jan 2016 09:33:25 -0800 (PST) In-Reply-To: X-Mailman-Approved-At: Fri, 08 Jan 2016 08:10:23 -0500 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:101369 > On Thu, Jan 7, 2016 at 11:14 AM, Joseph Fox-Rabinovitz wrote: > > 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 s= ame module, but that is not relevant as far as I can tell). The following b= it of code illustrates the situation: > > __a =3D 3 > class B: > def __init__(self): > global __a > self.a =3D __a > b =3D 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 i= n a container whose name is not mangled. For example, doing `self.a =3D glo= bals()['__a']` or manually creating a dictionary with a non-mangled name an= d accessing that. > > I feel that there should be some way of accessing __a within the class di= rectly in Python 3. Clearly my expectation that global would fix the issue = is incorrect. I would appreciate either a solution or an explanation of wha= t is going on that would convince me that accessing a module attribute in s= uch a way should be forbidden. > > -Joseph Fox-Rabinovitz > > P.S. For reference, the Stack Overflow question is here: http://stackover= flow.com/questions/34621484/how-to-access-private-variable-of-python-module= -from-class One more detail that makes me think that name mangling may be getting greedy to the point of bugginess: __a =3D 3 class B: def __init__(self): m =3D sys.modules[__name__] self.a =3D m.__a b =3D B() Raises the same exception as all the other way I tried to access __a: 'module' object has no attribute '_B__a'! -Joseph