473,219 Members | 2,007 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,219 software developers and data experts.

setattr getattr confusion

Hi,
Here's some code, it's broken:
class Key( object ):
def __init__(self):
self.props = KeyProps()
def __getattr__(self, v):
return getattr( self.props,v )
def __setattr__(self,var,val):
object.__setattr__(self.props,var,val)

class KeyProps(object):
def __init__(self):
self.x="NOT SET YET"
k1=Key()

It does not run because of the recursion that happens, but I don't know how
to lay this out.

I am trying to set the x value within props within Key:
k1.x="DAMN"
print k1.x

It seems to work, but it's really making a new 'x' in k1.
print k1.props.x
Shows "NOT SET YET", thus proving it failed to set x here.

I want to change k1.props.x by k1.x="something new" -- can this be done?

\d

Dec 8 '07 #1
6 4271
So you might want to describe your use-case.
Um.. I wanted an object with Key to hold other data. I wanted a way to set
that *other* data within Key without having to specify the "object
in-between" everytime.

k1.x = "ni!"

should perform:
k1.props.x = "ni!"

and
print k1.x
should perform:
print k1.props.x
I'll go look at your link. Thanks.

\d
Dec 8 '07 #2
On Dec 8, 6:06 am, Donn Ingle <donn.in...@gmail.comwrote:
Hi,
Here's some code, it's broken:

class Key( object ):
def __init__(self):
self.props = KeyProps()
def __getattr__(self, v):
return getattr( self.props,v )
def __setattr__(self,var,val):
object.__setattr__(self.props,var,val)
If you define __setattr__ you can't initialize attributes the ordinary
way. Instead, use self.__dict__. (Once it's initialized, you can
refer to it in the ordinary way.) So do it like this:

class Key(object):
def __init__self):
self.__dict__['props'] = KeyProps()
def __getattr__(self,var):
return getattr(self.props,var)
def __setattr__(self,var,val):
setattr(self.props,var,val)
Carl Banks
Dec 8 '07 #3
On Sat, 08 Dec 2007 14:26:00 +0200, Donn Ingle wrote:
>So you might want to describe your use-case.
Um.. I wanted an object with Key to hold other data. I wanted a way to
set that *other* data within Key without having to specify the "object
in-between" everytime.
That's called "automatic delegation".
--
Steven
Dec 8 '07 #4
class Key(object):
def __init__self):
self.__dict__['props'] = KeyProps()
Okay - that's weird. Is there another way to spin this?
def __setattr__(self,var,val):
setattr(self.props,var,val)
Perhaps by changing this one?

\d

Dec 8 '07 #5
Donn Ingle a écrit :
>>class Key(object):
def __init__self):
self.__dict__['props'] = KeyProps()

Okay - that's weird.
No, that's coherent. The default behavior (I mean, when there's no
descriptor involved etc) of __setattr__ is to store attributes in
instance.__dict__. So as long a you override __setattr__, you have to
take care of this by yourself.
Is there another way to spin this?
>>def __setattr__(self,var,val):
setattr(self.props,var,val)

Perhaps by changing this one?
If you know by advance which names should live in your object and/or
which should belong to the KeyProps instance, then you can check and
dispatch, ie:
class Key(object):
# names that must not be delegated to instance.props
_mynames = ['props', 'foo', 'bar']

def __setattr__(self, name, value):
if name in self._mynames:
object.__setattr__(self, name, value)
else:
setattr(self.props, name, value)

Dec 8 '07 #6
Thanks Bruno, I had to keep coding, so I used the long form
[Object.subobject.property = blah] anyway. It's long-winded, but
unambiguous.

\d

Dec 8 '07 #7

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

Similar topics

4
by: Gerson Kurz | last post by:
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...
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...
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 >>>...
4
by: Emin | last post by:
Dear experts, I got some unexpected behavior in getattr and copy.deepcopy (see transcript below). I'm not sure if this is actually a bug in copy.deepcopy or if I'm doing something too magical...
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"). ...
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...
0
by: John Nagle | last post by:
Just noticed, again, that getattr/setattr are ASCII-only, and don't support Unicode. SGMLlib blows up because of this when faced with a Unicode end tag: File...
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
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.