Functional Functions and the Python Singleton Unpattern

Jul 07, 2007 22:47

Have you ever written a module that looked like this?
subscribers = []

def addSubscriber(subscriber):
    subscribers.append(subscriber)

def publish(message):
    for subscriber in subscribers:
        subscriber.notify(message)

And then used it like this?
from publisher import publish

class worker:
    def work(self):
        publish(self)

I've ( Read more... )

Leave a comment

Comments 4

(The comment has been removed)

glyf July 8 2007, 09:13:07 UTC
I'm curious, how did you test the Singleton classes before using this pattern? At work we actually ask people during interviews to describe their experiences with singletons, and people have consistently reported that testing is highly problematic, and control of scope even more so.
It's Python. Some things are horrible, but everything is testable. For example, consider this little gem:
class Thing ( ... )

Reply

(The comment has been removed)

glyf July 8 2007, 20:54:45 UTC
Oh, sure, it's tedious and unpleasant in Java, but hey - you are using Java, clearly you like tedious and unpleasant things :). But it's often possible even without bytecode hacking.

For example, libraries like JMock achieve similar, if not identical, results by using java.lang.reflect.Proxy and friends.

(And who says bytecode manipulation has to be hard.)

Reply


Leave a comment

Up