[Tutor] Classes!Classes!Classes!

Denis spirou@carolo.net
Mon, 20 Dec 1999 21:33:09 +0100

R Parker wrote:
> Hello fellow members of this informative and helpful group.

Go on, I love when you speak like this ... ;-)

> I have been having a lot of trouble with Python as of late

Not possible

> and wondered if you could help me.

It will not be as easy as learning python

> I have studied and studied from books, tutorials and other such
> materials and have yet to figure how to use classes and what they do.

If that's true, don't shout it too loud ;-)

> (...) I would like to know the basics of classes and all the features about
> them. I would really appreciate it if you can help me. Reminder: I am a
> first time programmer and Python is my first programming language(so try
> to make it as simple as possible)

I wrote a simple of example for my daughter. She's not yet 12.
Tell us if it's that kind of example you're looking for.
Of course, my daughter got all the explanations a cheerfull daddy can
And you won't have those. Moreover you'll find french names as we are
Is it self explanatory ?

If you love that kind of small example and you understand french, you
could become a beta tester for my book : "Approach of programmation with
(yet to be written).
For our children, Python is terrific for the homeworks ! :-) 

class rectangle:
    l = 0
    L = 0
    def __init__(self, longueur, largeur):
        self.L = longueur
        self.l = largeur

    def perimetre(self):
        return "(%d + %d) * 2 = %d" % (self.L, self.l, 
                                             (self.L + self.l)*2)
    def surface(self):
        return "%d * %d = %d" % (self.L, self.l, self.L*self.l)

    def mesures(self):
        print """Un %s de %d sur %d""" % (self.__class__.__name__,
self.L, self.l)
        print """a une surface de %s""" % (self.surface(),)
        print """et un perimetre de %s\n""" % (self.perimetre(),)

class carre(rectangle):
    def __init__(self, cote):
        rectangle.__init__(self, cote, cote)

if __name__ == "__main__":
    r1 = rectangle(15, 30)
    r2 = rectangle(9, 17)
    c1 = carre(10)