Bug in string.find; was: Re: Proposed PEP: New style indexing,was Re: Bug in slice type

Paul Rubin http
Tue Aug 30 05:10:24 EDT 2005


Bryan Olson <fakeaddress at nowhere.org> writes:
>      Specifically, to support new-style slicing, a class that
>      accepts index or slice arguments to any of:
> 
>          __getitem__
>          __setitem__
>          __delitem__
>          __getslice__
>          __setslice__
>          __delslice__
> 
>      must also consistently implement:
> 
>          __len__
> 
>      Sane programmers already follow this rule.

It should be ok to use new-style slicing without implementing __len__
as long as you don't use $ in any slices.  Using $ in a slice without
__len__ would throw a runtime error.  I expect using negative
subscripts in old-style slices on objects with no __len__ also throws
an error.

Not every sequence needs __len__; for example, infinite sequences, or
sequences that implement slicing and subscripts by doing lazy
evaluation of iterators:

  digits_of_pi = memoize(generate_pi_digits())  # 3,1,4,1,5,9,2,...
  print digits_of_pi[5]   # computes 6 digits and prints '9'
  print digits_of_pi($-5)  # raises exception



More information about the Python-list mailing list