I’m trying to make an example of Base class
from this site
and having one problem with the line
assert 0, “Shape is an abstract base class”
python 2.5 is giving some error on this line
and tried several things but not getting out of this error!
in the following class
#########################
class Shape:
def init(self, name):
self.name = name
def displayName(self):
print ('I am ’ + self.name)
def draw(self):
assert 0, “Shape is an abstract base class”
#######################
can you tell me how to correct this and make it work in 2.5 python
and i check out the python page on this and
does not tell much on what it does and how
can you offer another explanation for this assert commands?
may be i can replace this with another equvialent line but not certain what!
Cuz when i try the complete script, everything is fine
(except the prints, you corrected it too)
assert means… give an error if the first thing is False. And 0 is False too
so something like
if not 0:
raise AssertionError("bla")
at least if you don’t use python optimization, then it will take out of the code completly
You can also replace it with a simple
pass
which tells python to do nothing in that function, without giving a error.
A Abstract class is an incomplete class. A class where a function is missing. Since Python don’t really got abstract classes there is just a function that will always give an error. it is not intended to get used. The function is only there to define how it looks. The real implementation you then have in Square/Circle/Canvas.
With that you can have a list of Shapes where you got Square/Circle/Canvas things in. Cuz of the draw in Shape you know that you can always call a draw on them. But it is not intended to ever use a Shape self.