Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:not': 0.03; 'string': 0.09; 'forcing': 0.09; 'literal': 0.09; 'subject:Why': 0.09; 'way:': 0.09; '(note': 0.16; '-tkc': 0.16; 'dictionary.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'notation.': 0.16; 'subject: \n ': 0.16; 'subject:accessing': 0.16; ':-)': 0.16; 'so.': 0.16; 'wrote:': 0.18; "python's": 0.19; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'asked': 0.31; 'gives': 0.31; 'another': 0.32; 'cases': 0.33; 'subject:with': 0.35; "can't": 0.35; 'but': 0.35; 'done': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'to:addr:python- list': 0.38; 'ability': 0.39; 'to:addr:python.org': 0.39; 'skip:a 30': 0.61; 'skip:m 50': 0.68; 'subject:there': 0.68; 'received:50.22': 0.84; 'subject:being': 0.84; 'ugly,': 0.84; 'was:': 0.91 Date: Wed, 4 Dec 2013 05:25:15 -0600 From: Tim Chase To: python-list@python.org Subject: Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? In-Reply-To: References: <15912943-29a1-4365-b027-7bb8cec447f8@googlegroups.com> <17gt99hg615jfm7bdid26185884d2pfdkf@4ax.com> <080d6a56-588b-425f-8968-8f77bc330427@googlegroups.com> <549180f1-fb98-4b59-b92f-5beceb1a6fb5@googlegroups.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386156238 news.xs4all.nl 2891 [2001:888:2000:d::a6]:55120 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61011 On 2013-12-04 21:33, Chris Angelico wrote: > I don't think so. What the OP asked for was: > > my_object.'valid-attribute-name-but-not-valid-identifier' > > Or describing it another way: A literal string instead of a token. > This is conceivable, at least, but I don't think it gives any > advantage over a dictionary. In both cases (attribute-access-as-dict-functionality and attribute-access-as-avoiding-setattr), forcing a literal actually diminishes Python's power. I like the ability to do a[key.strip().lower()] = some_value setattr(thing, key.strip().lower(), some_value) which can't be done (?) with mere literal notation. What would they look like? a.(key.strip().lower()) = some_value (note that "key.strip().lower()" not actually a "literal" that ast.literal_eval would accept). That's pretty ugly, IMHO :-) -tkc