Singleton Class Exception

Dan Lenski dlenski at gmail.com
Mon Nov 13 13:05:04 EST 2006


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




More information about the Python-list mailing list