[Tutor] Classes v2, thoughts/suggestions please

Paul Melvin paul at assured-networks.co.uk
Sat Jul 12 19:56:09 CEST 2008


> -----Original Message-----
> From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
> Behalf Of Alan Gauld
> Sent: 12 July 2008 18:08
> To: tutor at python.org
> Subject: Re: [Tutor] Classes v2, thoughts/suggestions please
> 
> 
> "Paul Melvin" <paul at assured-networks.co.uk> wrote
> 
> >  i have come up with a working class for a square,
> 
> Well done. It might not be the most elegant solution on the
> inside but it isn't too bad from the users point of view and thats
> what you should aim for IMHO.
> 
> > I was thinking about using *args to get maybe the colour
> > information in,
> 
> I would define polygon to take a list of points as a parameter
> of init so you put the onus on the user to pass in  tbe points
> in a list. That makes the init method easy:
> 
> Class Polygon:
>     def __init__(self, pList, filled=True, line_width=1):
>         self.points = pList
>         self.filled = filled
>         self.line_width = line_width
> 
> Your draw method is as before but instead of repeating
> the code 4 times just put it into a loop that iterates over
> the points list, something like:
> 
> def draw(self):
>     if self.filled:
>         glBegin(GL_QUADS)
>     else:
>         glLineWidth(self.line_width)
>         glBegin(GL_LINE_LOOP)
>     for p in self.pList
>         glColor4f(p.col[0], p.col[1], p.col[2], p.col[3])
>         glVertex2i(int(p.x), int(p.y))
>     glEnd()
>     if not filled and line_width != 1:  # reset to default
>         glLineWidth(1)
> 
> Note that I put it as the draw method not as a separate
> function. There is little point in having a class then calling
> a separate function to which you have to pass all the
> data in the class!
> 
> > And how can i go about testing that i get appropriate values during
> > the
> > testing/building phase (generally speaking), is it lots of
> > print/return
> > statements and then remove them?
> 
> I'm not sure what you mean by "appropriate values" in this context.
> Since you are drawing graphics I suspect you will have to just
> draw them in a simple canvas and visually ensure the result is
> what you expect?
> 
> You could try implementing a __str__ method in each class that
> lets you print out a nicely formatted report of the Point and/or
> Polygon data, which would allow you to do:
> 
> p = Point(.....)
> print p
> s = Polygon([p,p2,p3,p4])
> print s
> 
> That would allow you to see what the graphic parameters will
> be before drawing.
> 
> HTH,
> 
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
Thanks again for your help, as for 'appropriate' values, in this specific
case it would be numbers but I am also interested in the general design
process, ie how you check your code during design/coding, is it print
statements for example?

Before I start on my polygon class, Alan did mention that it was not
'elegant' which i know as I am only starting and I thought of it as dirty
but working, can someone tell me how I could improve this code or write
better code in the future

Cheers

paul
 

__________ Information from ESET Smart Security, version of virus signature
database 3263 (20080711) __________

The message was checked by ESET Smart Security.

http://www.eset.com
 



More information about the Tutor mailing list