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

Jerry Hill malaclypse2 at gmail.com
Wed Dec 4 15:58:21 EST 2013


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)

Today (in python 3.3), it prints 9, because my_object.foo is 10, the
local variable bar is equal to 1, and 10 minus 1 is 9..  Under your
new proposal, it would print 1000, right?  Is there any way for your
proposal to be backwards compatible?

-- 
Jerry



More information about the Python-list mailing list