By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

Note about getattr and '.'

P: n/a
There is an interesting skewness in python:

class A(object): pass
>>a=A()
setattr(a, '$foo', 17)
getattr(a, '$foo')
17

But I can't write
>>a.'$foo'
Nov 21 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sz****@gmail.com wrote:
There is an interesting skewness in python:

class A(object): pass
>>>a=A()
setattr(a, '$foo', 17)
getattr(a, '$foo')
17

But I can't write
>>>a.'$foo'
Yes, this is known. I think IronPython uses a specialized dictionary for members, which prohibits
malformed names. I don't know if there will be such a dictionary in any future CPython version.
(Would be good.)

panzi
Nov 21 '06 #2

P: n/a

sz****@gmail.com wrote:
There is an interesting skewness in python:

class A(object): pass
>a=A()
setattr(a, '$foo', 17)
getattr(a, '$foo')
17

But I can't write
>a.'$foo'
Likewise, you can write

setattr(a,'hello',17)

but you can't write

a.'hello'

but you can write

a.hello

The only thing that can follow the "." (attribute access) operator is a
valid Python identifier. Strings may not. Anyways, it's probably not
a good idea in most cases to use getattr and setattr with a
non-identifier, but Python doesn't prevent you from doing it. (Which
is good in the occasional case where it is a good idea.)

BTW, Matlab has an interesting syntax; you could have written this:

a.("$foo")

(And because Matlab is weird that way, this was not structure member
access, but some weird hybrid of structure accces and indexing and/or
slicing. The syntax was very useful in Matlab because there's no
built-in associative array and using this syntax was the easiest way to
get something like it. In Python we'd do that with a dict, so it's not
so useful here.)
Carl Banks

Nov 21 '06 #3

P: n/a
Le 21 Nov 2006 13:21:52 -0800,
sz****@gmail.com a écrit :
There is an interesting skewness in python:

class A(object): pass
>a=A()
setattr(a, '$foo', 17)
getattr(a, '$foo')
17

But I can't write
>a.'$foo'
Well, you can even do :
>>class A(object): pass
....
>>a = A()
setattr(a, 'some.attr', 15)
a.some.attr
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'A' object has no attribute 'some'
>>getattr(a, 'some.attr')
15
>>>
Which is a lot funnier IMHO :)

- Jon
Nov 21 '06 #4

P: n/a
On Tue, 21 Nov 2006 22:39:09 +0100, Mathias Panzenboeck wrote:
sz****@gmail.com wrote:
>There is an interesting skewness in python:

class A(object): pass
>>>>a=A()
setattr(a, '$foo', 17)
getattr(a, '$foo')
17

But I can't write
>>>>a.'$foo'

Yes, this is known. I think IronPython uses a specialized dictionary for members, which prohibits
malformed names. I don't know if there will be such a dictionary in any future CPython version.
(Would be good.)

Why would it be good?

How many bugs have you found that were caused by this behaviour?

--
Steven D'Aprano

Nov 22 '06 #5

P: n/a

Steven D'Aprano wrote:
On Tue, 21 Nov 2006 22:39:09 +0100, Mathias Panzenboeck wrote:
Yes, this is known. I think IronPython uses a specialized dictionary for members, which prohibits
malformed names. I don't know if there will be such a dictionary in any future CPython version.
(Would be good.)

Why would it be good?

How many bugs have you found that were caused by this behaviour?
It's not bugs. A specialized dictionary could be better optimized if
you know it can only hold Python identifiers. There's talk of such a
dictionary in Python 3000.
Carl Banks

Nov 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.