Why are index() and count() only for mutable sequences?

M.-A. Lemburg mal at lemburg.com
Fri Jul 9 03:53:00 EDT 1999


Gerrit Holl wrote:
> 
> On Thu, Jul 08, 1999 at 09:09:44PM +0200, M.-A. Lemburg wrote:
> > Date:         Thu, 08 Jul 1999 21:09:44 +0200
> > From: "M.-A. Lemburg" <mal at lemburg.com>
> > To: Bob Alexander <bobalex at ix.netcom.com>
> > Cc: python-list at python.org
> > Subject: Re: Why are index() and count() only for mutable sequences?
> >
> > Bob Alexander wrote:
> > >
> > > There is no modification of the sequence performed by these functions
> > > -- it seems as though they could be available for all sequences. It
> > > would certainly be useful for tuples as well as lists (strings, too,
> > > for that matter, even though we have the more general string.find()).
> >
> > You could try count(), index(), forall() and exists() from mxTools:
> >
> >       http://starship.skyport.net/~lemburg/mxTools.html
> >
> > Albeit, they work with conditions (functions that return 1/0) to
> > implement the testing process.
> >
> 
> Is this an anwser??
> He asked: "why"...

Well, count() and index() are methods not functions, so the
question was a little misleading. You cannot add methods to
*all* sequences, the only way to do this is by defining a
function which takes a sequence argument (*). That's why I gave
the hint to mxTools.

(*) This might change in future versions of Python though,
once types (classes coded in C) support the normal class
mechanisms too -- you could then add the methods to the base
class.

Cheers,
-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                   175 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/






More information about the Python-list mailing list