[Tutor] class and objects and such
Sean 'Shaleh' Perry
Fri, 14 Dec 2001 21:45:44 -0800 (PST)
> No specific size, shape, or even function, a class defines the general
> properties of a entire family opf object,s and you then create specific
> objects with refinement of the definition of the general properties, and
> special bellial bells, whistles, and GONGS for that indivigual item.
> OK so far?
> arg. how do I use this? ReadREadReadReadRead...
You are attacking the probem too hard.
OO is just about creating nouns and giving them the ability to act. Almost
like creating little robots. It takes some practice. After you make a few bad
designs you realize what works.
A common example is a geometry one.
In C (pascal, etc) if you wanted to draw a shape you had to call a function
like 'draw_square(sq)'. If you wanted a triangle you had to call triangle
functions. Each time what you were doing and what you were doing it with were
tied together. If you suddenly needed trapezoids you had to go and find
trapezoid functions and change you code. OO ties the verbs to the nouns.
'draw_square' becames 'square.draw'. This way you can have all of the shapes
knowing how to draw themselves and the code does not care what shape it is.
You may be thinking this is a little esoteric. So let's come back to a
concrete example you use every day in python. When you do 'for i in thing' you
are using the power of OO. The 'in' operator works by calling the
__getitem__() member of thing. So as long as thing supports __getitem__ you
can loop over it easily.