Event triggering and weak references

Gerald Klix gklix at hassler.de
Sun Apr 30 16:12:03 EDT 2000


Ricardo Nogueira wrote:

> First thanks for this beautiful language : )
>
> I am Smalltalk addicted : )  and this means that I can't do with any other
> language, ex. C, C++, Java,  : (
> This was true until I found Python 2 weeks ago at Sydney Uni. I loved its
> modularity, pluggin behaviour and freedom.
> But I am missing Smalltalk event handling... Smalltalk (like Python) support
> the call-Back mechanism, but there is a more powerful inter-object interest
> registering that I haven't found in Python (so far).
>
> This is what I would like to do:
> Imagine I could have two or more Widgets open displaying a car speed value,
> say a numerical and a graphic display, if this car speed changes (ex. some
> one pushes the break), I would like to have any open widget on this value
> updated (on opening each window would register interest with this car
> speed).
>
> Smalltalk implements this with:
>
> The widget would register interest in one aspect of the car  (on opening
> over aCar):
>   aCar when:#speedChanged send: #updateSpeedDisplay to: self
>
> And the car would just notify who is interested, if any (when its speed
> changes):
>   self trigger: #speedChanged
>
> But if any of this windows is not referenced anymore or crashes I would like
> the garbage collector clear it (the car would not be notified and his
> pointer to the window shouldn't keep the garbage collector form freeing the
> window memory).
>
> Again, Smalltalk implements this with weak references, that doesn't count
> for the garbage collector.
>
> Is there support for this in Python?
>
> Any suggestions?

Having the same background and facing the same problem, I wrote a small module
called "subject" with one simple class called "Subject".
Here it is:
--- snip --
##############################################################################
#
"""This module implements stuff for observed python objects, called
subjects."""
#
# $Header: /cvsroot/python/pss/subject.py,v 1.3 2000/02/23 14:45:59 bear Exp $
#
##############################################################################

__version__ = "1.1"
__revision__ = '$Revision: 1.3 $'[11:-2]

class Subject:
 """This class represents an observed subject."""

 def __init__( self ):
  """Initialize the subject."""
  self.observers = []

 def attach( self, observer ):
  """Add an observer to the subject.
  Observers are callable objects."""
  self.observers.append( observer )

 def detach( self, observer ):
  """Remove an observer."""
  self.observers.remove( observer )

 def notify( self, facet = None ):
  """Notifiy all observers of the subjects
  change in facet."""
  for observer in self.observers:
      observer( self, facet )

 def detachAll( self ):
  """Detach all observers."""
  self.observers = []


##############################################################################
#
# $Log: subject.py,v $
# Revision 1.3  2000/02/23 14:45:59  bear
# Added a detach all method.
#
# Revision 1.2  1998/10/22 21:33:03  bear
# Changed or added revsion and version info.
# Added module documentation strings.
# Started wrapping proxy.
#
# Revision 1.1  1998/10/20 18:57:35  bear
# Finished test code. Started real work.
#
##############################################################################
--- snip --
Gerald Klix




More information about the Python-list mailing list