alan.gauld at freenet.co.uk
Wed Feb 2 09:32:59 CET 2005
> I was just wondering, what magic can you do with classes?
You can define your own types. Thats what classes are for.
Those types can be as 'magic' as your imagination
(and programming skills!) allow.
> other classes are interesting to subclass? I've seen Object too, but
> don't understand what it does.
It's 'object' - lowercase o - and that is the way to tell
Python to use "new-style" classes, which are not very new now!
Essentially all the basic types in Python are descended from object
and the object class provides access to some special features that
won't be available if you don't subclass object (as in classic style
classes) OTOH subclsassing object does add overhead to your class
and so makes it a bit slower.
You can also subclass any other Python type, thus
class bigfloat(float): ....
would provide a special type of floating point
number - maybe with unlimited precision, provided
you wrote the code to allow that...
Finally classes are themselves objects and can be used as a
way of monitoring and controlling a collection of objects.
This is often called meta-programming and if you want to
twist your head in knots read the paper that Guido wrote
on meta programming in Python :-)
Read the OOP topic in my tutorial for more about using classes.
(but not about meta programming!! :-)
Author of the Learn to Program web tutor
More information about the Tutor