<[EMAIL PROTECTED]> wrote

1. What is the difference between a classmethod and a
staticmethod, and when would i use either?

Kent pointed you at the recent discussion but from your later
comments I thibnk its pretty safe to say that for now you don't
need to care. The vast majority of classes you build will not
use either of them.

2. I want to create an object, presumably a class, to handle
attributes of an open gl object.

You define a class and then create objects as instances of the class.
The class is a template for creating lots of similar objects. If it
helps you can think of it a bit like defining a function that creates
an empty dictionary. Here is an example of a point class:

def makePoint(x=0,y=0):
   newFoo = { 'X': x, 'Y': y }
   return newFoo

p1 = newFoo(22,66)
p2 = newFoo()

print p1['X']   # prints 22
print p2['Y']    # prints 0 - the default value

This is equivalent to

class Point:
   def __init__(self, x=0, y=0):
       self.x = x
       self.y = y

p1 = Point(22,66)
p2 = Point()

print p1.x
print p2.y

Notice that while it takes more typing to define the class
it takes a lot less to use it - no quotes needed etc. This is a
lot of what OO is about defining structures(classes() that are
easy to use and hide a lot of effort. In fact, in Python, classes
can be thought of as just a very clever type of dictionary.

The other thing about classes is that as well as data they
contain methods. You can make a dictionary hold methods
too but using them is, again, trickier than using the methods
of a class.

For example, if i wanted to create a 2d square i need 4 points,
each with an x,y co-ord, and each point could also have an
optional colour

Or you could have just two diameterically opposite points and
calculate where the others are! - this is how most GUI toolkits
do it. (In fact for a square you can get away with one point plus
a length!) You could also consider a square to be a set of four
lines each with 2 points. The choice of representation is all yours.
One of the key benefits of OO is that the user of your class doesn't
even need to know or care if you provide enough methods...

Also, do the points have colours? Or do the lines have colours?
Or does the square have a single border colour? Again, the choice
is yours. And if you provide the methods the user need not know
nor care.

, in the form (1.0, 1.0, 1.0), each of these items is then passed
to the 'drawing' function to actually draw the square or polygon etc.

So to define a class to your spec we could define a POint:

class Point:
   def __init__(self, x, y, col):
       self.x = x
       self.y = y
       self.col = col

class Square:
    def __init__(self,p1,p2,p3,p4):
         self.points = [p1,p2,p3,p4]
    def draw(self):
         myTookKitDrawSquare(p1.x,p1.y, p1.col,
                                           p2.x,p2.y, p2.col,
                                           p3.x,p3.y, p3.col,
                                           p4.x,p4.y, p4.col)


s1 = Square(Point(0,0,'red'), Point(0,55, 'blue'), Point(55,0,'green'),Point(55,55,'red')) s2 = Square(Point(0,0,'white'), Point(0,5, 'grey), Point(5,0,'green'),Point(5,5,'black'))

for shape in [s1,s2]:
    shape.draw()

My question is how do i design the appropriate class to do this,
i have coded these with lists at the moment

Does the above (untested!) example give you an idea? (Its far from
perfect but deliberately made as simple as I could and as close to
your model as I could)

There is no great magic about OOP its just a convenient way of
packaging data and the functions that operate on that data into
a single entity - this is encapsulation. You can then pass the
resultant objects around as a unit. You can also ignore whats
inside and focus on using the methods - this is called abstraction.
And if a number of classes share the same set of methods
you can even ignore the types of the objects(within the group)
and this is whats called polymorphism. Lots of jargon for what
are basically simple concepts

open gl uses the same attribute name for each, ie glVertex2i
and glColor3f, and am not sure how to pass this correctly,

I'm no OpenGL expert and not sure I understand your
question here.

   glColor3f(1.0, 0.0, 0.0)
   glVertex2i(l[0], l[1])

These would I think be the equivalent of the function that
I called "myTookKitDrawSquare" in the example above.

This is based on a square, but i would like to change the
number of vertices using a for loop, i am just unsure how
to get the variables into the correct places.

You could define a polygon class that takes list of points
as its constructor. [Note: Thre is a long running debate in
OOP circles about whether squares etc are subtypes
rectangles which are subtypes of polygons or peers of
them. Its very philosophical and a bit like asking whether
the chicken or egg came first! Beware of analysis paralysis!]

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to