473,671 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setattr using invalid attribute names - bug or feature?

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

class test:
pass

instance = test()
setattr(instanc e, "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 2011
Gerson Kurz wrote:
I stumbled across this (while using my homebrewn enum class):

class test:
pass

instance = test()
setattr(instanc e, "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(instanc e, "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(instanc e, "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(instanc e, "THIS :*2+~# IS OBVIOUSLY INVALID")
print a

123

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

class test:
pass
instance = test()
setattr(instanc e, "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*******@doit yourself.com> wrote in message
news:9c******** *************** **@posting.goog le.com...
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(instanc e, "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(instanc e, "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
2714
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++ ? thanks, Eric
5
2866
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 itself, what I've to pass to setattr as first argument?
11
6587
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
4
1463
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 is attribute eggs. I need to assign a new value to eggs. In other words, I need to perform the following: A.spam.eggs=newValue The problem is that I have only a string s='spam.eggs' with which to work, so I cannot write an expression written...
10
1994
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"). Thanks.
25
2564
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 example: with quitCommandButton .enabled = true .default = true end with
0
1193
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 lazy variable can be stored in a variety of places, Database, PyTables etc. The lazy variable is a large variable and so I dont want to hold it in memory all of the time, I d rather just get it when needed and then store it for future work. Most...
4
1390
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 __init__(self): self.B = B() class B(object):
4
10476
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?
0
8473
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
8390
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
8911
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...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
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
7428
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
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
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
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.