[Python-Dev] Remove str.find in 3.0?

Wolfgang Lipp paragate at gmx.net
Sat Aug 27 15:16:13 CEST 2005


kay,

your suggestion makes perfect sense for me, i haven't actually tried
the examples tho. guess there could be a find() or index() or
indices() or iterIndices() ??? function 'f' roughly with these arguments:

def f( x, element, start = 0, stop = None, default = _Misfit, maxcount =  
None, reverse = False )

that iterates over the indices of x where element (a substring, key, or
value in a sequence or iterator) is found, raising sth. like IndexError
when nothing at all is found except when default is not '_Misfit'  
(mata-None),
and starts looking from the right end when reverse is True (this *may*
imply that reversed(x) is done on x where no better implementation is
available). not quite sure whether it makes sense to me to always return
default as the last value of the iteration -- i tend to say rather not.
ah yes, only up to maxcount indices are yielded.

bet it said that passing an iterator for x would mean that the iterator is  
gone
up to where the last index was yielded; passing an iterator is not
acceptable for reverse = True.

MHO,

_wolf



On Sat, 27 Aug 2005 14:57:08 +0200, Kay Schluehr <kay.schluehr at gmx.net>  
wrote:
>
> def keep(iter, default=None):
>      try:
>          return iter.next()
>      except StopIteration:
>          return default
>
> Together with an index iterator the user can mimic the behaviour he
> wants. Instead of a ValueError a StopIteration exception can hold as
> an "external" information ( other than a default value ):
>
>  >>> keep( "abcdabc".index("bc"), default=-1)  # current behaviour of the
>                                                # find() function
>  >>> (idx for idx in "abcdabc".rindex("bc"))   # generator expression
>
>
> Since the find() method acts on a string literal it is not easy to
> replace it syntactically. But why not add functions that can be hooked
> into classes whose objects are represented by literals?
>
> def find( string, substring):
>      return keep( string.index( substring), default=-1)
>
> str.register(find)
>
>  >>> "abcdabc".find("bc")
> 1
>
> Now find() can be stored in a pure Python module without maintaining it
> on interpreter level ( same as with reduce, map and filter ).
>
> Kay







More information about the Python-Dev mailing list