why len(list) instead of list.len() ?
holger krekel
pyth at devel.trillke.net
Tue Jun 18 13:07:09 EDT 2002
Emile van Sebille wrote:
> "Markus Jais" <mjais at web.de> wrote in message
> news:EhBP8.1$Qn1.1124 at news.ecrc.de...
> > hello
> >
> > I way wondering why one has to write
> >
> > list = [2, 3, 5]
> > print len(list)
> >
> > instead of list.len()
> >
> > in Ruby I can write
> >
> > array = [2, 3, 4]
> > puts array.length()
> >
> >
>
> Because a two-line wrapper goes a long way?
>
> Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> class myList(list):
> ... len = list.__len__
> ...
> >>> l = myList([1,2,3,4])
> >>> l
> [1, 2, 3, 4]
> >>> l.len()
> 4
> >>> l.extend([5,6,7])
> >>> l.len()
> 7
this 'hands-on fix' has come up before, too. It bears the problem that
you would have to wrap every list that you work with. Otherwise you get nasty
inconsistencies.
Actually i think that there might a solution for the 'why does len not
work as expected'- problem. Once the builtin types cannot not only be
extended by inheritance but also by directly binding names in the types
attribute space. Something like:
list.len=list.__len__
could then really fix this problem. Of course there would still be major
combats over the question whether it should be included by default :-)
holger
More information about the Python-list
mailing list