Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail 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:not': 0.03; 'operator': 0.03; 'syntax': 0.04; 'attribute': 0.07; 'wednesday,': 0.07; 'string': 0.09; "'.'": 0.09; 'attributes': 0.09; 'classes.': 0.09; 'degree,': 0.09; 'identifier': 0.09; 'namespace': 0.09; 'obj': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'jan': 0.12; 'itself.': 0.14; 'changes': 0.15; 'backward': 0.16; 'declaration': 0.16; "module's": 0.16; 'namespace?': 0.16; 'piotr': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:accessing': 0.16; 'undesirable': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'module': 0.19; 'pointed': 0.19; '(the': 0.22; '>>>': 0.22; 'import': 0.22; 'preferred': 0.22; 'proposed': 0.22; 'header:User-Agent:1': 0.23; 'right.': 0.26; 'this:': 0.26; 'skip:_ 20': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'thus': 0.29; "i'm": 0.30; 'code': 0.31; 'consequence': 0.31; 'workaround': 0.31; 'yes.': 0.31; 'class': 0.32; 'checked': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'subject:with': 0.35; "can't": 0.35; 'created': 0.35; 'december': 0.35; 'objects': 0.35; 'point.': 0.35; 'but': 0.35; 'there': 0.35; 'subject:?': 0.36; 'being': 0.38; 'skip:o 20': 0.38; 'needed': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'major': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'break': 0.61; 'new': 0.61; 'received:173': 0.61; 'name': 0.63; 'refer': 0.63; 'great': 0.65; 'believe': 0.68; 'subject:there': 0.68; 'believe,': 0.84; 'keystrokes': 0.84; 'received:fios.verizon.net': 0.84; 'subject:being': 0.84; 'ugly,': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Date: Wed, 04 Dec 2013 22:21:06 -0500 References: <15912943-29a1-4365-b027-7bb8cec447f8@googlegroups.com> <17gt99hg615jfm7bdid26185884d2pfdkf@4ax.com> <5eb566a0-3911-48fa-ba83-a863da66a55d@googlegroups.com> <451d174b-6620-476d-bf84-a76c6b1de87e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 In-Reply-To: <451d174b-6620-476d-bf84-a76c6b1de87e@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386213683 news.xs4all.nl 2831 [2001:888:2000:d::a6]:51612 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61074 On 12/4/2013 6:42 PM, Piotr Dobrogost wrote: > On Wednesday, December 4, 2013 11:11:56 PM UTC+1, Terry Reedy wrote: >> The discussion of keystrokes is also a side-track. > > To great degree, yes. Having said that I find extra 11 keystrokes > needed to access some attributes to be a freaking big and > unjustifiable number. Given that there is almost no need to ever use operator chars in attribute names and given that syntax changes have the major undesirable consequence of backward incompatibility, I find it to be a small and inconsequential number. >> What you are proposing, I believe, is a new grammatical category: >> attribute-name := identifier or string-literal. This would break >> the symmetry of the grammatical form identifier '.' identifier and >> change it to the asymmetrical identifier '.' attribute-name, and >> that is the > > Nice description. > >> To put it another way, how does 'obj' get the non-standard >> attribute 'value-1', when obj is a module or class? The workaround >> given above for module attributes will not work for classes. The module workaround, which I find pretty ugly, is this: >>> len is __builtins__.len True >>> __globals__ = __import__(__name__) >>> a = 1 >>> a is __globals__.a True I have not checked that the import trick will work when a module is imported, but I believe it will. > I'm not sure I see your point. Do you mean that being inside class > declaration there's no name that referrs to the current namespace > (the way __globals__ refer to module's namespace) thus we can't use > proposed syntax to access non-standard attributes from this > namespace? Right. Class objects are not created until after the class code runs. >> Not really. As others have pointed out, getattr is the preferred >> way to get the value of an attribute when you have an object with >> attributes and a run-time-only reference to the name in a string >> variable. > > Yes, and I think it's very unfortunate in itself. Do you prefer obj.__dict__['name'] to getattr(obj, 'name')? -- Terry Jan Reedy