Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > getter and setter and list appends

Reply
Thread Tools

getter and setter and list appends

 
 
dasacc22
Guest
Posts: n/a
 
      04-20-2009
Hi,

I seem to be having a problem with a list being share across multiple
instantiations of it and dont quite understand why this is happening.

My class looks like this,

class Widget(object):
_parent = None
_children = []

def __init__(self, parent=None):
self.parent = parent

@property
def children(self):
return self._children

@property
def parent(self):
return self._parent

@parent.setter
def parent(self, obj):
if obj:
obj._children.append(self)
self._parent = obj


now if i make instances and attach children like so

a = Widget()
b = Widget(a)
c = Widget(a)
d = Widget(c)

Basically all the objects end up sharing the _children list from `a`
instead of forming something like a tree. Any advice would be greatly
appreciated.

Thanks,
Daniel
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-20-2009
dasacc22 wrote:

> Hi,
>
> I seem to be having a problem with a list being share across multiple
> instantiations of it and dont quite understand why this is happening.
>
> My class looks like this,
>
> class Widget(object):
> _parent = None
> _children = []
>
> def __init__(self, parent=None):
> self.parent = parent
>
> @property
> def children(self):
> return self._children
>
> @property
> def parent(self):
> return self._parent
>
> @parent.setter
> def parent(self, obj):
> if obj:
> obj._children.append(self)
> self._parent = obj
>
>
> now if i make instances and attach children like so
>
> a = Widget()
> b = Widget(a)
> c = Widget(a)
> d = Widget(c)
>
> Basically all the objects end up sharing the _children list from `a`
> instead of forming something like a tree. Any advice would be greatly
> appreciated.


The problem stems from you confusing instance attributes with class
attributes. You use a latter, which is shared amongst *all* instances of a
given class.

If you want instance-attributes, use

def __init__(self, ..):
self.children = []

also, your use of properties for the children is pointless in Python. Just
use "children" as attribute name. Then, if at one fine day you want to do
something more than just returning that value, you can still do that witout
changing the interface by using a property - as you do so for parent.

Diez
 
Reply With Quote
 
 
 
 
dasacc22
Guest
Posts: n/a
 
      04-20-2009
Ah thank you for clarifying, I did confuse instance and class
attributes from creating the list in the class def. I actually just
spiffed up that class to represent a portion of a much larger class
that needs getter and setter for children. Doing as you said fixed my
problem, heres the code as reference for w/e

class Widget(object):
_children = None
_parent = None

def __init__(self, parent=None):
self.children = []
self.parent = parent

@property
def children(self):
return self._children

@children.setter
def children(self, obj):
self._children = obj

@property
def parent(self):
return self._parent

@parent.setter
def parent(self, obj):
if obj:
print obj
obj.children.append(self)
self._parent = obj


On Apr 20, 1:05*pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> dasacc22 wrote:
> > Hi,

>
> > I seem to be having a problem with a list being share across multiple
> > instantiations of it and dont quite understand why this is happening.

>
> > My class looks like this,

>
> > class Widget(object):
> > * * _parent = None
> > * * _children = []

>
> > * * def __init__(self, parent=None):
> > * * * * self.parent = parent

>
> > * * @property
> > * * def children(self):
> > * * * * return self._children

>
> > * * @property
> > * * def parent(self):
> > * * * * return self._parent

>
> > * * @parent.setter
> > * * def parent(self, obj):
> > * * * * if obj:
> > * * * * * * obj._children.append(self)
> > * * * * * * self._parent = obj

>
> > now if i make instances and attach children like so

>
> > a = Widget()
> > b = Widget(a)
> > c = Widget(a)
> > d = Widget(c)

>
> > Basically all the objects end up sharing the _children list from `a`
> > instead of forming something like a tree. Any advice would be greatly
> > appreciated.

>
> The problem stems from you confusing instance attributes with class
> attributes. You use a latter, which is shared amongst *all* instances of a
> given class.
>
> If you want instance-attributes, use
>
> def __init__(self, ..):
> * * self.children = []
>
> also, your use of properties for the children is pointless in Python. Just
> use "children" as attribute name. Then, if at one fine day you want to do
> something more than just returning that value, you can still do that witout
> changing the interface by using a property - as you do so for parent.
>
> Diez


 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      04-20-2009
dasacc22 wrote:
> Hi,
>
> I seem to be having a problem with a list being share across multiple
> instantiations of it and dont quite understand why this is happening.


Class attributes are shared by all instances.

> My class looks like this,
>
> class Widget(object):
> _parent = None
> _children = []


Move this line
>
> def __init__(self, parent=None):
> self.parent = parent


to here.
>
> @property
> def children(self):
> return self._children
>
> @property
> def parent(self):
> return self._parent
>
> @parent.setter
> def parent(self, obj):
> if obj:
> obj._children.append(self)
> self._parent = obj
>
>
> now if i make instances and attach children like so
>
> a = Widget()
> b = Widget(a)
> c = Widget(a)
> d = Widget(c)
>
> Basically all the objects end up sharing the _children list from `a`
> instead of forming something like a tree. Any advice would be greatly
> appreciated.
>
> Thanks,
> Daniel
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      04-24-2009
>>>>> dasacc22 <> (d) wrote:

>d> Ah thank you for clarifying, I did confuse instance and class
>d> attributes from creating the list in the class def. I actually just
>d> spiffed up that class to represent a portion of a much larger class
>d> that needs getter and setter for children. Doing as you said fixed my
>d> problem, heres the code as reference for w/e


>d> class Widget(object):
>d> _children = None
>d> _parent = None


You still have them as class variables here. Now they are only used as
defaults because you assign to them in the instances so the instance
variables will be created then. But I think it is still confusing to
have these class variables here that you never use as such. Maybe this
is some leftover from Java experience where you do declare instance
variables at the class level?

>d> def __init__(self, parent=None):
>d> self.children = []
>d> self.parent = parent


>d> @property
>d> def children(self):
>d> return self._children


>d> @children.setter
>d> def children(self, obj):
>d> self._children = obj


What is the added value of using a property for the children attribute?
Why not just use an instance variable directly? Also a Java inheritance?

--
Piet van Oostrum <>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email:
 
Reply With Quote
 
dasacc22
Guest
Posts: n/a
 
      04-24-2009
On Apr 24, 4:04*am, Piet van Oostrum <p...@cs.uu.nl> wrote:
> >>>>> dasacc22 <dasac...@gmail.com> (d) wrote:

> >d> Ah thank you for clarifying, I did confuse instance and class
> >d> attributes from creating the list in the class def. I actually just
> >d> spiffed up that class to represent a portion of a much larger class
> >d> that needs getter and setter for children. Doing as you said fixed my
> >d> problem, heres the code as reference for w/e
> >d> class Widget(object):
> >d> * * _children = None
> >d> * * _parent = None

>
> You still have them as class variables here. Now they are only used as
> defaults because you assign to them in the instances so the instance
> variables will be created then. But I think it is still confusing to
> have these class variables here that you never use as such. Maybe this
> is some leftover from Java experience where you do declare instance
> variables at the class level?
>
> >d> * * def __init__(self, parent=None):
> >d> * * * * self.children = []
> >d> * * * * self.parent = parent
> >d> * * @property
> >d> * * def children(self):
> >d> * * * * return self._children
> >d> * * @children.setter
> >d> * * def children(self, obj):
> >d> * * * * self._children = obj

>
> What is the added value of using a property for the children attribute?
> Why not just use an instance variable directly? Also a Java inheritance?
>
> --
> Piet van Oostrum <p...@cs.uu.nl>
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org


Hi, yes, you are right, this is from previous experience, and thank
you for bringing this out. It would be better suited to move those
class variables to a comment to stay comfortable perhaps or eliminate
them altogether.

The property method of parent and children actually calls a
_set_as_parent() and _set_as_child() method after setting the private
variable to pack the object for display purposes so that children can
be detached from the parent (becoming its own parent) as a gui event.
 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      04-24-2009
>>>>> dasacc22 <> (d) wrote:

>d> The property method of parent and children actually calls a
>d> _set_as_parent() and _set_as_child() method after setting the private
>d> variable to pack the object for display purposes so that children can
>d> be detached from the parent (becoming its own parent) as a gui event.


Oh, I saw that in the parent setter but not in the children setter.
--
Piet van Oostrum <>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email:
 
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
problem with getter and setter not working Adam Sandler ASP .Net 12 05-26-2006 04:03 PM
C# webservices proxy generates public member variables and no getter/setter methods?? Steve ASP .Net Web Services 1 03-24-2006 02:50 AM
Static fields accessed by getter/setter...what happens? Stacey Java 1 02-10-2004 02:28 PM
Tired of 100s of stupid Getter/Setter methods Timo Nentwig Java 75 01-18-2004 05:17 AM
May I do data type conversion within getter/setter? Raoul Markus Java 3 09-20-2003 06:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57