[Tutor] Dumb question
alan.gauld@bt.com
alan.gauld@bt.com
Sun, 20 Jan 2002 17:50:40 -0000
> I'm not a programmer, so this is a kinda dumb question.
Its not dumb at all. In fact its something experienced
programmers often find harder to understand than beginners!
> What exactly is a class?
When programming it often helps to build a kind of
model of the "real world problem". Classes are descriptions
of the real world 'objects' that we are modelling.
This is similar to the techniquie of drawing a scale
diagram to measure somethjing, or building a scale model
for windtunnel testing used in other engineering disciplines.
Classes are the blueprints for our "scale model" objects.
> Why would use one?
To try to keep the solution as much like the problem as
possible - it helps us focus on the important bits of
the problem without getting bogged down in details
(which are hidden inside the class!)
> How do you define one?
class Pen:
# define properties of the class here
ink = 'black'
# also define operations on the objects here
# just like normal functions but inside the class
def draw(self, x,y): # ignore self for now but we need it...
# code to draw here
Now we can create an instance of a Pen:
p = Pen()
Draw with it
p.draw(5,9)
Describe it
print "Ink color is ", p.ink
and so forth. We can forget about the details of how to
draw(I dodn't show them anyhow!) and just think about
using pens.
> alot of reading on Python and this whole concept escapes me.
I dunno if it included my tutor but i have a topic on
Object orientation and examples of its use in both the
Event Driven chapter and the GUI chapter plus the Case Study
Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld