Classes and Python

Steve Holden sholden at holdenweb.com
Thu Jan 24 12:39:39 EST 2002


"Lawrence Oluyede" <rhymes at myself.com> wrote ...
> On 24 Jan 2002 08:34:28 -0800, aahz at panix.com (Aahz Maruch) wrote:
>
> >The real meaning is that if you're using the current Python 2.2, it's
> >wrong.  ;-)
>
> So, the tut is obsolete?
>
Not obsolete, just a little outdated. Since you appear to be an articulate
learner, I'm sure you could help to revise it. At that point in the tutorial
it probably isn't wise to put too much detail. There have been many changes
in the language recently (the last twelve months), and it's inevitable that
some materials are not fully up to date.

> >But it's probably still worthwhile to explain it:
> >
> >Let's suppose you create a list with
> >
> >>>> L=[]
> >>>> L
> >[]
> >
> >now you can add a new element to L with
> >
> >>>> L.append('foo')
> >>>> L
> >['foo']
> >
> >this means that L has object-like behavior, in that there are methods
> >that can be used.  But you cannot subclass from the list type in
> >versions of Python prior to 2.2 -- this is called the type/class split.
> >Python 2.2 takes the first steps to heal the type/class split, and you
> >*can* now inherit from the list type.
>
> Can u make a useful example with List subclassing?
>
Take a look at the UserList library module: this is what you used to have to
do to provide a class which modeled the functionality of a type. The type
has to be "wrapped", so that each of the type's methods were available as
methods of class instances, so subclasses could override them if necessary.

I haven't yet made an extended study of subclassing built-in types, so I'll
leave the examples to others.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list