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

Ethan Furman ethan at stoneleaf.us
Wed Dec 4 22:02:45 CET 2013


On 12/04/2013 12:58 PM, Jerry Hill wrote:
> On Wed, Dec 4, 2013 at 3:35 PM, Piotr Dobrogost
> <p at google-groups-2013.dobrogost.net> wrote:
>> Right. If there's already a way to have attributes with these "non-standard" names (which is a good thing) then for uniformity with dot access to attributes with "standard" names there should be a variant of dot access allowing to access these "non-standard" named attributes, too.
>
> Given the follow code, what do you think should print?
>
> class My_Class(object):
>      pass
>
> bar = 1
> my_object = My_Class()
> setattr(my_object, 'foo', 10)
> setattr(my_object, 'bar', 100)
> setattr(my_object, 'foo-bar', 1000)
>
> print(my_object.foo-bar)

Actually, under his proposal it would be:

   print(my_object."foo-bar")

and it would print 1000, while yours would still print 9.

--
~Ethan~



More information about the Python-list mailing list