473,663 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1359
sz****@gmail.co m 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.co m 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,'hell o',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.co m 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.co m 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
2067
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 the named attributed of object. name must be a string. If the string is the name of one of the object's attributes,
8
1897
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 >>> getattr(Parrot, "__dict__") is Parrot.__dict__ False
13
4178
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 object doesn't has actually this property return something else. In my case, I can't use getattr(object, property, default_value).
4
2413
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 string
3
1370
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
1552
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
20296
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
3070
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 Chapter 4 and reading about "Getting Object References with getattr". First, it say in there that, "you can get a reference to a function without knowing its name until run−time, by using the getattr function". That is the first thing that is a...
8
7956
by: Gregor Horvath | last post by:
Hi, class A(object): test = "test" class B(object): a = A() In : B.a.test
0
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8548
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.