473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Note about getattr and '.'

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
5 1352
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

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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Johnny | last post by:
Hi, I wonder what is the difference between the built-in function getattr() and the normal call of a function of a class. Here is the details: getattr( object, name) Return the value of...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
13
by: Pierre | last post by:
Hi, Sorry in advance, english is not my main language :/ I'd like to customize the result obtained by getattr on an object : if the object has the requested property then return it BUT if the...
4
by: Hole | last post by:
Hi There! I'm trying to use Zope and the product OpenFlow. I got the following error while I was using the built-in function getattr() to retrieve an OpenFlow object: attribute name must be...
3
by: Ayaz Ahmed Khan | last post by:
I'm working with the following class heirarchy (I've snipped out the code from the classes): class Vuln: def __init__(self, url): pass def _parse(self): pass
3
by: Jm lists | last post by:
Hello, Since I can write the statement like: Test whether a path is a directory Why do I still need the getattr() func as below? Test whether a path is a directory
1
by: Sergio Correia | last post by:
This works: # Module spam.py import eggs print getattr(eggs, 'omelet')(100) That is, I just call the function omelet inside the module eggs and evaulate it with the argument 100.
3
numberwhun
by: numberwhun | last post by:
Hello everyone! I am presently going through the "Dive Into Python" tutorial which I obtained from their website. No, this is not for any class, I am self-learning the Python language. I am in...
8
by: Gregor Horvath | last post by:
Hi, class A(object): test = "test" class B(object): a = A() In : B.a.test
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.