Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > setattr using invalid attribute names - bug or feature?

Reply
Thread Tools

setattr using invalid attribute names - bug or feature?

 
 
Gerson Kurz
Guest
Posts: n/a
 
      06-14-2004
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.)
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-14-2004
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
 
Reply With Quote
 
 
 
 
Yermat
Guest
Posts: n/a
 
      06-14-2004
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

 
Reply With Quote
 
Meno
Guest
Posts: n/a
 
      06-14-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Gerson Kurz) wrote in message news:<(E-Mail Removed)-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.
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      06-14-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> (E-Mail Removed) (Gerson Kurz) wrote in message

news:<(E-Mail Removed)-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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
recursion error using setattr and getattr Nathan Harmston Python 0 06-07-2007 11:23 AM
setattr for secondary attribute Alex Python 3 11-21-2005 11:32 PM
Re: Using pickle with setattr dataangel Python 3 10-15-2004 01:48 AM
Using pickle with setattr dataangel Python 0 10-13-2004 03:43 AM
Matching attribute names to element names in a different path Carl XML 0 04-01-2004 01:15 PM



Advertisments