By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Singleton Class Exception

P: n/a
Hello List,

I would like to make a singleton class in python 2.4.3, I found this
pattern in the web:

class Singleton:
__single = None
def __init__( self ):
if Singleton.__single:
raise Singleton.__single
Singleton.__single = self
the line "raise Singleton.__single" invokes in my class the following
error:

exceptions must be classes, instances, or strings (deprecated), not
PurchaseRequisitionController

Greetings, Dennis

Nov 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
dischdennis wrote:
Hello List,

I would like to make a singleton class in python 2.4.3, I found this
pattern in the web:

class Singleton:
__single = None
def __init__( self ):
if Singleton.__single:
raise Singleton.__single
Singleton.__single = self
the line "raise Singleton.__single" invokes in my class the following
error:

exceptions must be classes, instances, or strings (deprecated), not
PurchaseRequisitionController

Greetings, Dennis
The problem is that you yoinked a bad example of a singleton. The
error is correct, only exceptions derived from the Exception class and
strings are supported. (Strings are supported for historical usage,
and a deprecated.)

Why exactly do you need a singleton? Do you want only one instance of
a class? Often, you really want something that shares state: all are
one. This pattern is known as a borg. Please see
"http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531" to see
how the Borg Pattern works.

If you absolutely gotta have a singleton, there are other ways of
implementing it. I can think of only one reason: You want the
singleton's destructor to be called only if there are no remaining
instances in existance. (For the Borg pattern, the deconstructor can
be called once per instance created.)

The problem with this reason is that Python doesn't guarantee when the
destructors will be called for instances (and some instances will never
have their destructors called). Although C-Python implements usually
destroys an instance when its ref-count drops to 0, IronPython and
Jython may do things very differently. (I have no experience with
either of those, so I cannot tell you how they differ.)

Still, if you're dead set on a Singleton class, I'll post one in a
little bit.

--Jason

Nov 13 '06 #2

P: n/a
dischdennis wrote:
the line "raise Singleton.__single" invokes in my class the following
error:

exceptions must be classes, instances, or strings (deprecated), not
PurchaseRequisitionController
Denis,
Jason's explanation is correct! You are trying to use the Singleton
instance as the exception, which is not only confusing but illegal.
The argument of raise should be an instance of a class derived from
Exception. You should define some kind of informative Exception class,
e.g.:

class SingletonException(Exception):
pass

class Singleton:
__single = None
def __init__( self ):
if Singleton.__single:
raise SingletonException()
Singleton.__single = self

foo = Singleton() # works
bar = Singleton() # raises SingletonException

Dan

Nov 13 '06 #3

P: n/a
I threw together two different singleton classes. They both ensure
that the initializers and destructors can be called at most once. (The
ImmortalSingleton won't ever have its destructor called unless the
programmer manually forces things.)

I haven't extensively tested these things, so handle this code like a
rabid wildebeest. Observe that Python may not call your destructors
when you expect. IAnd again, you probably want the Borg pattern.

Still, this was an interesting exercise to do.

--- Code Starts ---
class MortalSingleton(object):
"""This class implements a 'mortal' singleton pattern. Classes
derived
from this class cannot be directly instantiated or take arguments in
their
initializers.

The GetSingleton() class method returns a reference to a single
instance,
or creates a single instance as needed.

This class only keeps a weak reference to the single instance. This
means
that, if all hard references are destroyed, the instance can be
destroyed
by Python (and the __del__ method *could* be called, depending on
implementation). Thus, the single-ton is mortal.

This could be used as a mix-in class, assuming that the other classes
do not over-ride the __new__() method (which prevents willy-nilly
instantiation).

Note that if you need all instances to share state, you probably want
to use the Borg pattern.

Comments on this travesty are appreciated. *grin*
"""
def __new__(cls, *args, **keyargs):
# Raise a run-time error preventing direct instantiation
raise RuntimeError(
'Do not instantiate %s directly. Use the
%s.GetSingleton()'
'class method.' % (cls.__name__, cls.__name__)
)

@classmethod
def GetSingleton(cls):
from weakref import ref

retObject = getattr(cls, '_instance', lambda : None)()
if retObject is None:
# Create a new object with the given class
retObject = object.__new__(cls)

# The initializer must be manually called in this case
retObject.__init__()
cls._instance = ref(retObject)
return retObject

class ImmortalSingleton(object):
"""This class implements a classic 'immortal' singleton pattern.
Classes derived from this class will allow only one instance to exist.

Since the class caches a hard reference to the single pattern, it
doesn't die unless the programmer gets rid of all references and
manually
deletes the cache reference.

Note that you probably want to use a variant of the Borg class rather
than
this."""
def __new__(cls, *args, **keyargs):
# Raise a run-time error preventing direct instantiation

raise RuntimeError(
'Do not instantiate %s directly. Use the
%s.GetSingleton()'
'class method.' % (cls.__name__, cls.__name__)
)

@classmethod
def GetSingleton(cls):
retObject = getattr(cls, '_instance', lambda : None)()
if retObject is None:
# Create a new object with the given class
retObject = object.__new__(cls)

# The initializer must be manually called in this case
retObject.__init__()
cls._instance = retObject
return retObject

Nov 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.