473,382 Members | 1,441 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,382 software developers and data experts.

setattr using invalid attribute names - bug or feature?

I stumbled across this (while using my homebrewn enum class):

class test:
pass

instance = test()
setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)

I would've expected some kind of error message here when calling
setattr(); after all, its not a regular attribute? Plus, documentation
says

"
Set a named attribute on an object; setattr(x, 'y', v) is equivalent
to
``x.y = v''.
"

and you cannot write this:

instance.THIS :*2+~# IS OBVIOUSLY INVALID = 123

(oh, and its: PythonWin 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200
32 bit (Intel)] on win32.)
Jul 18 '05 #1
4 1995
Gerson Kurz wrote:
I stumbled across this (while using my homebrewn enum class):

class test:
pass

instance = test()
setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)

I would've expected some kind of error message here when calling
setattr(); after all, its not a regular attribute?


Okay. But so what?

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
Gerson Kurz wrote:
I stumbled across this (while using my homebrewn enum class):

class test:
pass

instance = test()
setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)

I would've expected some kind of error message here when calling
setattr(); after all, its not a regular attribute?

Okay. But so what?

-Peter


And sometime it is usefull to create some attributes that can unlikely
be used by the programmer (for example for cache or...).

I've seen code on coockbook that were using that property.

--
Yermat

Jul 18 '05 #3
ge*********@t-online.de (Gerson Kurz) wrote in message news:<40**************@news.t-online.de>...
I stumbled across this (while using my homebrewn enum class):

class test:
pass

instance = test()
setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)

I would've expected some kind of error message here when calling
setattr(); after all, its not a regular attribute? Plus, documentation
says

"
Set a named attribute on an object; setattr(x, 'y', v) is equivalent
to
``x.y = v''.
"

and you cannot write this:

instance.THIS :*2+~# IS OBVIOUSLY INVALID = 123


No, but you can write this:
a = getattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID")
print a

123

Meno.
Jul 18 '05 #4
You can also do this:

class test:
pass
instance = test()
setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)
print instance.__dict__["THIS :*2+~# IS OBVIOUSLY INVALID"]
123 print instance.__dict__.keys()
['THIS :*2+~# IS OBVIOUSLY INVALID'] print instance.__dict__.items()
[('THIS :*2+~# IS OBVIOUSLY INVALID', 123)]

The only reason that you cannot do:

instance.This :*2+~# IS OBVIOUSLY INVALID

is because attribute names accessed in this
fashion can't have spaces.

Larry Bates,
Syscon, Inc.

"Meno" <do*******@doityourself.com> wrote in message
news:9c*************************@posting.google.co m...
ge*********@t-online.de (Gerson Kurz) wrote in message

news:<40**************@news.t-online.de>...
I stumbled across this (while using my homebrewn enum class):

class test:
pass

instance = test()
setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)

I would've expected some kind of error message here when calling
setattr(); after all, its not a regular attribute? Plus, documentation
says

"
Set a named attribute on an object; setattr(x, 'y', v) is equivalent
to
``x.y = v''.
"

and you cannot write this:

instance.THIS :*2+~# IS OBVIOUSLY INVALID = 123


No, but you can write this:
a = getattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID")
print a

123

Meno.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric | last post by:
Slightly off topic, i know, but here goes: I'm trying to xlate a module of mine to C++. Only problem is, it makes heavy use of "setattr". Anyone know a straightforward way to do "setattr" in C++...
5
by: kramb64 | last post by:
I'm trying to use setattr inside a module. From outside a module it's easy: import spam name="hello" value=1 setattr(spam, name, value) But if I want to do this inside the module spam...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
4
by: Alex | last post by:
I apologize for asking maybe a very trivial question. I have a new class object A with slots. One of the slots is, for example, object spam. Object spam, in turn, also has slots and one of them...
10
by: Paulo da Silva | last post by:
Hi! In a class C, I may do setattr(C,'x',10). Is it possible to use getattr/setattr for variables not inside classes or something equivalent? I mean with the same result as exec("x=10"). ...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
0
by: Nathan Harmston | last post by:
Hi, I m trying to implement an object which contains lazy" variables. My idea is to alter the getattr and the setattr methods. However I keep on getting a recursion error. My idea is that the...
4
by: Rotlaus | last post by:
2 weeks ago i asked for a etended getattr() which worked really fine, but now i would love to have a extended setattr() as well. Lets assume i have some classes: class A(object): def...
4
by: maestro | last post by:
Why are these functions there? Is it somehow more idiomatic to use than to do obj.field ? Is there something you can with them that you can't by obj.field reference?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.