Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61074

Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers?
Date 2013-12-04 22:21 -0500
References (2 earlier) <eb15e1a8-49b1-4d55-a864-141efc65394e@googlegroups.com> <17gt99hg615jfm7bdid26185884d2pfdkf@4ax.com> <5eb566a0-3911-48fa-ba83-a863da66a55d@googlegroups.com> <mailman.3590.1386195130.18130.python-list@python.org> <451d174b-6620-476d-bf84-a76c6b1de87e@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3601.1386213683.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-03 09:14 -0800
  Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ned Batchelder <ned@nedbatchelder.com> - 2013-12-03 12:38 -0500
  Re: Why is there no natural syntax for accessing attributes with names
 not being valid identifiers? Dave Angel <davea@davea.name> - 2013-12-03 12:48 -0500
    Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-04 14:05 -0800
      Re: Why is there no natural syntax for accessing attributes with
 names not being valid identifiers? Dave Angel <davea@davea.name> - 2013-12-04 17:28 -0500
  Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-03 09:31 -0800
    Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-03 15:49 -0800
  Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? random832@fastmail.us - 2013-12-03 13:03 -0500
    Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-03 15:47 -0800
      Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Tim Chase <python.list@tim.thechases.com> - 2013-12-03 19:06 -0600
        Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-04 12:35 -0800
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-04 20:46 +0000
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Jerry Hill <malaclypse2@gmail.com> - 2013-12-04 15:58 -0500
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-04 13:02 -0800
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Neil Cerutti <neilc@norwich.edu> - 2013-12-04 21:41 +0000
            Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-04 13:54 -0800
              Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Neil Cerutti <neilc@norwich.edu> - 2013-12-06 14:07 +0000
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-06 08:51 -0800
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Rotwang <sg552@hotmail.co.uk> - 2013-12-07 00:47 +0000
            Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-04 14:13 -0800
              Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-04 15:09 -0800
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-04 15:57 -0800
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-04 16:26 -0800
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ned Batchelder <ned@nedbatchelder.com> - 2013-12-04 20:17 -0500
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Terry Reedy <tjreedy@udel.edu> - 2013-12-04 21:58 -0500
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-04 19:05 -0800
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Steven D'Aprano <steve@pearwood.info> - 2013-12-05 07:56 +0000
      Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Tim Roberts <timr@probo.com> - 2013-12-03 21:45 -0800
        Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? rusi <rustompmody@gmail.com> - 2013-12-03 22:31 -0800
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-12-04 01:57 -0700
            Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? rusi <rustompmody@gmail.com> - 2013-12-04 02:09 -0800
              Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-12-04 11:29 +0100
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? rusi <rustompmody@gmail.com> - 2013-12-04 04:01 -0800
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-12-04 13:32 +0100
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? rusi <rustompmody@gmail.com> - 2013-12-04 05:02 -0800
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-12-04 20:57 +0100
              Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Chris Angelico <rosuav@gmail.com> - 2013-12-04 21:33 +1100
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? rusi <rustompmody@gmail.com> - 2013-12-04 03:27 -0800
              Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Tim Chase <python.list@tim.thechases.com> - 2013-12-04 05:25 -0600
              Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-12-04 13:30 +0200
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Chris Angelico <rosuav@gmail.com> - 2013-12-04 22:40 +1100
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-04 07:56 -0800
              Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-12-04 19:43 -0700
                Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? rusi <rustompmody@gmail.com> - 2013-12-04 20:48 -0800
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-04 19:27 -0500
            Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Roy Smith <roy@panix.com> - 2013-12-04 20:03 -0500
        Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-04 01:12 -0800
        Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Roy Smith <roy@panix.com> - 2013-12-04 08:23 -0500
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-04 13:00 -0800
        Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-04 12:07 -0800
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-04 12:22 -0800
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-04 20:55 +0000
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Ethan Furman <ethan@stoneleaf.us> - 2013-12-04 13:00 -0800
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Terry Reedy <tjreedy@udel.edu> - 2013-12-04 17:11 -0500
            Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-12-04 15:42 -0800
              Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Terry Reedy <tjreedy@udel.edu> - 2013-12-04 22:21 -0500
          Re: Why is there no natural syntax for accessing attributes with names not being valid identifiers? Rotwang <sg552@hotmail.co.uk> - 2013-12-05 01:50 +0000

csiph-web