471,338 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 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 4196
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alex | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
10 posts views Thread by Paulo da Silva | last post: by
reply views Thread by Nathan Harmston | last post: by
4 posts views Thread by Rotlaus | last post: by
4 posts views Thread by maestro | last post: by
reply views Thread by rosydwin | last post: by

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.