Newbie Question: Abstract Class in Python

Ian Bicking ianb at colorstudy.com
Sun Jul 6 20:14:26 EDT 2003


On Sun, 2003-07-06 at 19:08, Kevin Bass wrote:
> I am new to Python and want to know how to implement an abstract class? I
> have read through different books and I have no found this information.
> Thanks!

There's no distinction between abstract and concrete classes in Python. 
So, say, you Figure class with a Square and a Circle subclass, you'd
simply do:

class Figure:
    ...

class Square(Figure):
    ...

class Circle(Figure):
    ...

I guess if you really wanted to be sure someone didn't try to
instantiate Figure, you could add something like:

class Figure:
    def __init__(self):
        assert self.__class__ is not Figure, \
            'Figure is an abstract class, do not instantiate it'







More information about the Python-list mailing list