Private class?

Kent Johnson kent37 at tds.net
Thu Apr 21 10:50:08 EDT 2005


codecraig wrote:
> class EventListener:
>     def eventOccurred(self, eventType): pass
> 

The EventListener class is not needed in Python; you can include it for its documentation value if 
you like but Python does not require interfaces the way Java does.

> class EventBus:
>     EVENT_GROW_LEAVES = 0
> 
>     __listeners = {}
> 
>     def register(listener, eventType):
>         if __listeners.has_key(eventType):
>             curListeners = __listeners.get(eventType)
>             curListeners.append(listener)
>         else:
>             __listeners[eventType] = [listener]
> 
>     def fire(eventType):
>         if __listeners.has_key(eventType):
>             x = __listeners.get(eventType)
>             for l in x:
>                 l.eventOccurred(eventType)
> 
> ...so that is what I am thinking.  However, i guess my issue is how to
> make EventBus a singleton or prevent it from being instaniated and
> making it's methods statically accessible.

One way to do this is to make an EventBus module and make __listeners, register() and fire() be 
module attributes. Your client code would look like
import EventBus
...
EventBus.register(...)
EventBus.fire(...)

Kent



More information about the Python-list mailing list