weakrefs to functions for observer pattern

Discussion in 'Python' started by Michael Schneider, Nov 3, 2005.

  1. Hello All,

    I am comming back to python after being away for several years.

    I would like to use weak refs in an observer pattern implementation.

    The problme that I have seems to be that weakrefs can't manage functions.

    ------------------- from docs:
    http://www.python.org/doc/current/lib/module-weakref.html

    Not all objects can be weakly referenced; those objects which can
    include class instances, functions written in Python (but not in C),
    methods (both bound and unbound), sets, frozensets, file objects,
    generators, type objects, DBcursor objects from the bsddb module,
    sockets, arrays, deques, and regular expression pattern objects. Changed
    in version 2.4: Added support for files, sockets, arrays, and patterns.

    -------------------------------------------------------

    Is there a technique that I can use to leverage weak references to I
    don't have to unregister my observers?

    Thanks
    Mike

    PS. here is the code that I have been working with (Note: I commendout
    out the weak ref creation.

    --------------------------------------------------------------------
    import types
    class Observable(object):

    def addObserver(self, observer, events=None):
    if not hasattr(self, '_observers'):
    #self._observers = weakref.WeakKeyDictionary()
    self._observers = {}

    if observer is None:
    return

    if events is not None and type(events) not in (types.TupleType,
    types.ListType):
    events = (events,)
    self._observers[observer] = events

    def removeObserver(self, callable):
    if not hasattr(self, '_observers'):
    return

    if self._observers.has_key(callable):
    del self._observers[callable]

    ##
    # Notify all currently-registered Observers.
    #
    # This observer will be called if the event is one that the
    # Observer is interested in, or if event is 'None'
    #
    # @param event The event to notify the Observers about. None
    # means no specific event.
    #
    # *args - standard arguments - passed through to observer
    # **kw - keyword arguments - passed through to observer
    def notifyObservers(self, event=None, *args, **kw):

    if not hasattr(self, '_observers'):
    return

    for cb, events in self._observers.items():
    if events is None or event is None or event in events:
    if cb is not None:
    cb(self, event, *args, **kw)
    Michael Schneider, Nov 3, 2005
    #1
    1. Advertising

  2. Michael Schneider <> wrote:

    > I would like to use weak refs in an observer pattern implementation.
    > The problme that I have seems to be that weakrefs can't manage functions.


    They can manage just fine functions written in *Python*, just not
    "builtin functions*, i.e., ones written in *C*. Just wrap any builtin
    function you need to register as observer into a tiny Python-coded
    wrapper and live happily ever after.
    ...
    > Not all objects can be weakly referenced; those objects which can
    > include class instances, functions written in Python (but not in C),



    Alex
    Alex Martelli, Nov 3, 2005
    #2
    1. Advertising

  3. Alex Martelli wrote:
    > Michael Schneider <> wrote:
    >
    >
    >>I would like to use weak refs in an observer pattern implementation.
    >>The problme that I have seems to be that weakrefs can't manage functions.

    >
    >
    > They can manage just fine functions written in *Python*, just not
    > "builtin functions*, i.e., ones written in *C*. Just wrap any builtin
    > function you need to register as observer into a tiny Python-coded
    > wrapper and live happily ever after.
    > ...
    >
    >>Not all objects can be weakly referenced; those objects which can
    >>include class instances, functions written in Python (but not in C),

    >
    >
    >
    > Alex

    Alex,

    Thank you, I mis-read the docs.

    The mistake I made was having was using a weak reference as a key in
    the dictionary.

    Weak references will be very useful for me.

    I really enjoy python. So many good things have been added to the
    language without taking the fun away :)

    Mike

    --
    The greatest performance improvement occurs on the transition of from
    the non-working state to the working state.
    Michael Schneider, Nov 3, 2005
    #3
  4. Michael Schneider <> wrote:
    ...
    > Thank you, I mis-read the docs.
    >
    > The mistake I made was having was using a weak reference as a key in
    > the dictionary.


    Rather than using a weakref.WeakKeyDictionary , I guess?


    > Weak references will be very useful for me.
    >
    > I really enjoy python. So many good things have been added to the
    > language without taking the fun away :)


    Glad to hear this!


    Alex
    Alex Martelli, Nov 3, 2005
    #4
    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. Beatrice Rutger
    Replies:
    0
    Views:
    702
    Beatrice Rutger
    Jun 5, 2005
  2. Replies:
    6
    Views:
    727
    Chris Uppal
    Feb 13, 2006
  3. mem

    Observer pattern

    mem, May 18, 2004, in forum: C++
    Replies:
    2
    Views:
    524
    adiian
    Jun 4, 2007
  4. Paolino

    An observer pattern application.

    Paolino, Aug 18, 2005, in forum: Python
    Replies:
    0
    Views:
    332
    Paolino
    Aug 18, 2005
  5. Replies:
    5
    Views:
    547
Loading...

Share This Page