Re: list property fires get on append
On Jan 6, 3:03 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > I've created a class that has a property which points at a private > > list. When I try to use the append() function on this list property, > > the fget method is fired rather than the fset method. If I directly > > set my property to a literal list, the set method fires. > > # this fires a get for some reason > > hierarchy.children.append( Hierarchy.Hierarchy()) > > that's the expected behaviour: you're *fetching* the "children" > attribute in order to modify it, you're not replacing it. > > reading up on Python's object model might be helpful. > > I figured that an append would be treated as a set since I'm adding to the list. But what you say makes sense, although I can't say I'm happy with the behaviour. Is there any way I can get the append to fire a set? I'm thinking of properties from my C# background where i believe that manipulation such this would be considered a set. -- http://mail.python.org/mailman/listinfo/python-list
Re: Point Object
On Jan 5, 6:37 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > I am nes to python and need some help. Can anyone lead me in the > right direction to create and print a Point object, and then use id to > print the object's unique identifier. Translate the hexadecimal form > into decimal and confirm that they match. > > Any help woul be much appreciated. > > Pete You shouldn't have to compare the hex IDs. Just a simple comparison operator will work: firstPoint = Point() secondPoint = Point() print(firstPoint == secondPoint) result: True -- http://mail.python.org/mailman/listinfo/python-list
Re: list property fires get on append
On Jan 6, 11:36 am, Carl Banks <[EMAIL PROTECTED]> wrote:
> On Sun, 06 Jan 2008 00:31:13 -0800, Soviut wrote:
> > I figured that an append would be treated as a set since I'm adding to
> > the list. But what you say makes sense, although I can't say I'm happy
> > with the behaviour. Is there any way I can get the append to fire a
> > set? I'm thinking of properties from my C# background where i believe
> > that manipulation such this would be considered a set.
>
> You'd have to have to hook into the container object itself to detect the
> modification. This might be pretty workable for you since it's an
> internal object. Basically, instead of using a list, use a special list-
> like object that notifies it's owner when it changes. Here's a simple
> example to point you in the right direction:
>
> class NotifierList(list):
> def __init__(self,*args,**kwargs):
> super(NotifierList,self).__init__(*args,**kwargs)
> self.watchers = []
> def add_watcher(self,watcher):
> self.watchers.append(watcher)
> def _notify_watchers(self):
> for watcher in self.watchers:
> watcher.object_changed(self)
> def append(self,value):
> super(NotifierList,self).append(value)
> self._notify_watchers()
> # override all other mutating calls, including __setitem__
> # left as exercise
>
> class Hierarchy(object):
> def __init__(self):
> self.children = NotifierList()
> self.children.add_watcher(self)
> def object_changed(self,obj):
> print "self.children modified"
> # no need to make children a property then
> # unless you want to trap rebinding it to new object also
>
> A couple other minor suggestions:
>
> print is a statement, not a function. You should write
>
> print "GETTING"
>
> not
>
> print("GETTING")
>
> The latter works, but it will cease to work if you want to print more
> than one thing. Note that print is scheduled to become a function in
> Python 3.0, but for now it's a statement.
>
> Based on the name of your class and typical usage, I'm guessing that you
> probably want _children to be an instance attribute rather than a class
> attribute, so I redid it that way, but .)
>
> P.S. Is calling a method called "firing" in C#?
>
> Carl Banks
Thanks for the help, there's a lot to digest there but I realized that
I was having issues with _children being a class attribute when I
noticed every single instance had the same _children list. I've since
moved things into my __init__ method.
Basically the reason I needed to use a property was to run a private
helper method that sets references to the parent and root nodes of my
hierarchy. Since I was simply appending to my children list, there
was no callback to tell the container to process the children.
And no, calling a method is not necessarily called "firing", but I've
been using the term a lot recently when dealing with events so it
seemed appropriate.
--
http://mail.python.org/mailman/listinfo/python-list
Re: list property fires get on append
On Jan 6, 11:36 am, Carl Banks <[EMAIL PROTECTED]> wrote:
> On Sun, 06 Jan 2008 00:31:13 -0800, Soviut wrote:
> > I figured that an append would be treated as a set since I'm adding to
> > the list. But what you say makes sense, although I can't say I'm happy
> > with the behaviour. Is there any way I can get the append to fire a
> > set? I'm thinking of properties from my C# background where i believe
> > that manipulation such this would be considered a set.
>
> You'd have to have to hook into the container object itself to detect the
> modification. This might be pretty workable for you since it's an
> internal object. Basically, instead of using a list, use a special list-
> like object that notifies it's owner when it changes. Here's a simple
> example to point you in the right direction:
>
> class NotifierList(list):
> def __init__(self,*args,**kwargs):
> super(NotifierList,self).__init__(*args,**kwargs)
> self.watchers = []
> def add_watcher(self,watcher):
> self.watchers.append(watcher)
> def _notify_watchers(self):
> for watcher in self.watchers:
> watcher.object_changed(self)
> def append(self,value):
> super(NotifierList,self).append(value)
> self._notify_watchers()
> # override all other mutating calls, including __setitem__
> # left as exercise
>
> class Hierarchy(object):
> def __init__(self):
> self.children = NotifierList()
> self.children.add_watcher(self)
> def object_changed(self,obj):
> print "self.children modified"
> # no need to make children a property then
> # unless you want to trap rebinding it to new object also
>
> A couple other minor suggestions:
>
> print is a statement, not a function. You should write
>
> print "GETTING"
>
> not
>
> print("GETTING")
>
> The latter works, but it will cease to work if you want to print more
> than one thing. Note that print is scheduled to become a function in
> Python 3.0, but for now it's a statement.
>
> Based on the name of your class and typical usage, I'm guessing that you
> probably want _children to be an instance attribute rather than a class
> attribute, so I redid it that way, but .)
>
> P.S. Is calling a method called "firing" in C#?
>
> Carl Banks
I just want to thank you for the example. I implemented the
NotifierList class and modified it to suite my project. It works
great. Thanks again.
--
http://mail.python.org/mailman/listinfo/python-list
