B
Ben Finney
Howdy all,
Okay, so Guido doesn't like Abstract Base Classes[0], and interfaces
are the way of the future[1]. But they're not here now, and I
understand ABCs better.
I want my modules to (sometimes) define an abstract base exception
class, that all other exceptions in that module inherit from.
class FooException(Exception):
""" Base class for all FooModule exceptions """
class FooBadFilename(FooException):
""" Raised when a bad filename is used in a foo """
class FooUnknownBar(FooException, KeyError):
""" Raised when an unknown bar is used with a foo """
However, there should never be an exception raised of class
FooException, and in fact I want that to cause an appropriate error to
be raised from the module.
Normally, I'd pick some key part of the functionality of the class,
and cause that to raise NotImplementedError. It's then the
responsibility of subclasses to override that.
However, in the case of exceptions, I don't want to override *any*
functionality; everything should be provided by the base classes. It'd
be messy to have to override something in every subclass just to
ensure the abstraction of the module base exception.
I've tried doing this in the __init__():
class FooException(Exception):
""" Base class for all FooModule exceptions """
def __init__(self):
raise NotImplementedError, \
"%s is an abstract class for exceptions" % self.__class__
When I use this, I discovered to my horror that the subclasses were
calling FooException.__init__ -- which I though wasn't supposed to
happen in Python!
It's also rather semantically weird, to my eye.
Can I do something tricky with checking base classes in the
FooException.__init__() ?
[0] Although he's apparently been quoted earlier as saying he did.
He's changed his mind[1] since then.
[1] <URL:http://www.artima.com/weblogs/viewpost.jsp?thread=92662>
Okay, so Guido doesn't like Abstract Base Classes[0], and interfaces
are the way of the future[1]. But they're not here now, and I
understand ABCs better.
I want my modules to (sometimes) define an abstract base exception
class, that all other exceptions in that module inherit from.
class FooException(Exception):
""" Base class for all FooModule exceptions """
class FooBadFilename(FooException):
""" Raised when a bad filename is used in a foo """
class FooUnknownBar(FooException, KeyError):
""" Raised when an unknown bar is used with a foo """
However, there should never be an exception raised of class
FooException, and in fact I want that to cause an appropriate error to
be raised from the module.
Normally, I'd pick some key part of the functionality of the class,
and cause that to raise NotImplementedError. It's then the
responsibility of subclasses to override that.
However, in the case of exceptions, I don't want to override *any*
functionality; everything should be provided by the base classes. It'd
be messy to have to override something in every subclass just to
ensure the abstraction of the module base exception.
I've tried doing this in the __init__():
class FooException(Exception):
""" Base class for all FooModule exceptions """
def __init__(self):
raise NotImplementedError, \
"%s is an abstract class for exceptions" % self.__class__
When I use this, I discovered to my horror that the subclasses were
calling FooException.__init__ -- which I though wasn't supposed to
happen in Python!
It's also rather semantically weird, to my eye.
Can I do something tricky with checking base classes in the
FooException.__init__() ?
[0] Although he's apparently been quoted earlier as saying he did.
He's changed his mind[1] since then.
[1] <URL:http://www.artima.com/weblogs/viewpost.jsp?thread=92662>