Observer implementations

Discussion in 'Python' started by Tobias Weber, Jun 15, 2009.

  1. Tobias Weber

    Tobias Weber Guest

    Hi,
    how to use the Observer pattern in Python?

    I found PubSub and PyDispatcher, both of which are abandoned. The former
    lives on inside wx, the latter as Louie. I have no idea how Louie is
    different, but it seems to be abandoned as well. There also is a
    PyDispatcher copy in wx.

    Despite the confusion all those are useable, but I ran into the problem
    that I can't register a @classmethod because weakref doesn't like them.

    --
    Tobias Weber
     
    Tobias Weber, Jun 15, 2009
    #1
    1. Advertising

  2. Tobias Weber wrote:
    > Hi,
    > how to use the Observer pattern in Python?


    Implement it in your classes?

    > I found PubSub and PyDispatcher, both of which are abandoned. [...]


    I haven't searched for these, but googling for "python observer pattern"
    yields http://code.activestate.com/recipes/131499/ and this seems like
    a good inspiritation for owns own implementation.

    -- Gerhard
     
    Gerhard Häring, Jun 15, 2009
    #2
    1. Advertising

  3. Tobias Weber

    Tobias Weber Guest

    In article <>,
    Gerhard Häring <> wrote:

    > Implement it in your classes?


    No time to reinvent the wheel

    I'd still need to know how to weakref a classmethod

    > yields http://code.activestate.com/recipes/131499/ and this seems like


    I'd prefer many-to-many

    --
    Tobias Weber
     
    Tobias Weber, Jun 16, 2009
    #3
  4. Tobias Weber

    Carl Banks Guest

    On Jun 15, 5:22 pm, Tobias Weber <> wrote:
    > In article <>,
    >  Gerhard Häring <> wrote:
    >
    > > Implement it in your classes?

    >
    > No time to reinvent the wheel


    Hmm, observer pattern seems to be one of those things simple enough
    that reimplementing the wheel might actually be less work than
    adapting a third-party implementation. But I'll leave that decision
    to you.


    > I'd still need to know how to weakref a classmethod


    I can't imagine why you would need to weak reference a class method,
    since they usually live forever along with the class. But again, you
    are doing it so you would know. I can't think of an easy way to do it
    without metaclass programming.

    Unless you are trying to create a weakref to a bound classmethod. I'd
    say that's still pretty useless: bound classmethods only keep alive
    classes and classmethods, which both live forever, and bound
    classmethods are lightweight and hardly worth the effort.

    Maybe show us what you need to weak reference classmethods for and we
    can help you better.


    Carl Banks
     
    Carl Banks, Jun 16, 2009
    #4
  5. Tobias Weber wrote:
    ....
    > No time to reinvent the wheel
    >
    > I'd still need to know how to weakref a classmethod
    >

    See PyDispatcher for code to do this.

    PyDispatcher, at least, is not abandoned, it would be more accurate to
    say "finished". I use it in OpenGLContext (extensively), but I haven't
    had to change anything in a rather long time. It pretty much just works.

    Enjoy,
    Mike

    --
    ________________________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://www.vrplumber.com
    http://blog.vrplumber.com
     
    Mike C. Fletcher, Jun 16, 2009
    #5
  6. Tobias Weber

    Tobias Weber Guest

    In article <>,
    "Mike C. Fletcher" <> wrote:

    > See PyDispatcher for code to do this.


    That was the original problem. Got it now: if used inside the class
    definition dispatcher.connect will raise "cannot create weak reference
    to 'classmethod' object". Outside (using Class.method) it works fine.

    --
    Tobias Weber
     
    Tobias Weber, Jun 16, 2009
    #6
  7. Tobias Weber wrote:
    > In article <>,
    > "Mike C. Fletcher" <> wrote:
    >
    >
    >> See PyDispatcher for code to do this.
    >>

    >
    > That was the original problem. Got it now: if used inside the class
    > definition dispatcher.connect will raise "cannot create weak reference
    > to 'classmethod' object". Outside (using Class.method) it works fine.
    >

    Ah, I think you've got a logic problem there. The classmethod during
    class creation has no reference to the class being created, so if you
    were to call *that* thing it would blow up:

    class x( object ):
    @classmethod
    def y( cls, text ):
    print text
    y( 'No you do not!' )

    if you were to create a reference to *that* thing (the decorated
    un-bound class method) it would never have the binding for cls, so it
    wouldn't do anything. The correct way IMO to bind after-class-creation
    (e.g. by a class decorator or metaclass) where you reference a bound
    class method. If you *really* want to do it this way, you can do
    something like this:

    class x( object ):
    @classmethod
    def y( cls, value ):
    pass
    dispatcher.connect( lambda *args: x.y( *args ), signal='hello' )

    but ick.

    HTH,
    Mike

    --
    ________________________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://www.vrplumber.com
    http://blog.vrplumber.com
     
    Mike C. Fletcher, Jun 17, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig
    Replies:
    1
    Views:
    504
    xarax
    Oct 27, 2003
  2. Timo Nentwig
    Replies:
    1
    Views:
    1,201
    Thomas Fritsch
    Apr 16, 2004
  3. Rogan Dawes
    Replies:
    4
    Views:
    798
  4. Steve Green

    Observer methods

    Steve Green, Apr 10, 2005, in forum: Java
    Replies:
    2
    Views:
    396
    Chris Uppal
    Apr 11, 2005
  5. Beatrice Rutger
    Replies:
    0
    Views:
    728
    Beatrice Rutger
    Jun 5, 2005
Loading...

Share This Page