Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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; 'argument': 0.05; 'attribute': 0.07; 'string': 0.09; "'.'": 0.09; 'attributes': 0.09; 'classes.': 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; 'python': 0.11; 'jan': 0.12; '*and': 0.16; 'identifiers': 0.16; 'ought': 0.16; 'partly': 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; 'variable.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'discussion': 0.18; 'module': 0.19; 'pointed': 0.19; '>>>': 0.22; 'preferred': 0.22; 'proposed': 0.22; 'header:User-Agent:1': 0.23; 'least': 0.26; 'asking': 0.27; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'that.': 0.31; 'breaking': 0.31; 'proposing': 0.31; 'workaround': 0.31; 'class': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'problem.': 0.35; 'add': 0.35; 'there': 0.35; 'subject:?': 0.36; 'detail': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'most': 0.60; 'break': 0.61; 'new': 0.61; 'received:173': 0.61; 'name': 0.63; 'skip:n 10': 0.64; 'subject:there': 0.68; 'funny': 0.74; 'believe,': 0.84; 'inherent': 0.84; 'keystrokes': 0.84; 'received:fios.verizon.net': 0.84; 'subject:being': 0.84; 'whereas': 0.91 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 17:11:56 -0500 References: <15912943-29a1-4365-b027-7bb8cec447f8@googlegroups.com> <17gt99hg615jfm7bdid26185884d2pfdkf@4ax.com> <5eb566a0-3911-48fa-ba83-a863da66a55d@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: <5eb566a0-3911-48fa-ba83-a863da66a55d@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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386195130 news.xs4all.nl 2858 [2001:888:2000:d::a6]:44757 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61056 On 12/4/2013 3:07 PM, Piotr Dobrogost wrote: You have proposed to make non-identifier attribute names 'official', rather than discouraged, by abbreviating > x = getattr(obj, 'value-1') or x = obj.__dict__['value-1'] # implementation detail as > x = obj.'value-1' The discussion of enlarging the scope of 'identifier' is not relevant as you are not proposing that. In particular, you are not asking that obj.value-1 get the 'value-1' attribute of obj. The discussion of keystrokes is also a side-track. 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 problem. Most identifiers are attributes of a namespace and many attributes are set *and accessed* as undotted names in module or class code. The symmetry is at least partly inherent and breaking it does not work very well. >>> len is __builtins__.len True >>> __globals__ = __import__(__name__) >>> a = 1 >>> a is __globals__.a True 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. >> Remember the Python philosophy that there ought to be one way to do >> it. > > Funny you use this argument against my idea as this idea comes from > following this rule whereas getattr goes against it. 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. It would also be the case that "obj.'value' is obj.value", so the proposal *would* add duplication. -- Terry Jan Reedy