473,387 Members | 1,520 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,387 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 4279
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?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.