assert command

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!

Thanks & happy 2.5

Can it be that the error says?

AssertionError: “Shape is an abstract base class”

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.

here is the error i get

File “obadd1.py”, line 17
assert 0,‘Shape is an abstract base class’
^
TabError: inconsistent use of tabs and spaces in indentation

don’t know since 2.5 i’m also beginning to get theses space and tab error more often!

how did you write it to make it work !

Thanks

You can create your class of errors:

class myError(Exception):     def __init__(self,value):[INDENT]         self.value = value
def __str__(self):         return repr(self.value)[/INDENT]

Next , i used this :


import myError
try:      print " No my error!"
except:     raise myError.myError("Error: This is my error!")

See more info on: http://stackoverflow.com/questions/944592/best-practice-for-python-assert

You may want to try something like myShape. Shape could already be a class.

@ Fado

how did you make it work ?

can you upload the code
i’ll test it
not certain what’s wrong with theses tab and space thing error

Thanks


class Shape:
    def __init__(self, name=""):
        self.name = name

    def draw(self):
        print("A derived class must override draw()")
        assert(0, "Shape is an abstract base class")


class Square(Shape):
    numOfSquares = 0
    def __init__(self, name=""):
        Square.numOfSquares += 1
        name = "Square."+str(Square.numOfSquares)
        Shape.__init__(self, name)

    def draw(self):
        print("Drawing %s" % (self.name))


class Circle(Shape):
    numOfCircles = 0
    def __init__(self, name=""):
        Circle.numOfCircles += 1
        name = "Circle."+str(Circle.numOfCircles)
        Shape.__init__(self, name)

    def draw(self):
        print("Drawing %s" % (self.name))


# COMPOSITION : HAS-A
class Canvas:
    def __init__(self, name):
        self.name = name
        self.objectlist = []

    def link(self, shape):
        self.objectlist.append(shape) 
    
    # Object + non-instance
    def __add__(self, shape):
        self.link(shape)

    # non-instance + Object
    def __radd__(self, shape):
        self.link(shape)

    # instance[index] or for use in loop context (for shape in canvasObject:)
    def __getitem__(self, index):
        return self.objectlist[index]


if __name__ == '__main__':
    sq1 = Square()
    sq2 = Square()

    ci1 = Circle()
    ci2 = Circle()

    canvas = Canvas("untitled")

    for shape in (sq1, sq2):
        canvas.link(shape)
 
    # Overriding __add__ and __radd__ respectively
    canvas + ci1
    #canvas.__add__(ci1) 

    ci2 + canvas
    #canvas.__radd__(ci2) 
    
    # Overriding __getitem__
    print(canvas[0].name)
    canvas[3].draw()

    # Overriding __getitem__
    for shape in canvas:
        shape.draw()

ok i got the little msitake and now it’s working like a charm

but still how does this erro works

i mean how is it generated ?

i’m trying to understand how this script works
and the concept are a little complicated to follow

but i;ll try again tomorrow to see the inner working
and when this can be used and under what circomstances

Thanks