[Python-Dev] Remove str.find in 3.0?
tjreedy at udel.edu
Wed Aug 31 05:58:41 CEST 2005
""Martin v. Löwis"" <martin at v.loewis.de> wrote in message
news:43100E14.6080009 at v.loewis.de...
> Terry Reedy wrote:
>> One (1a) is to give an inband signal that is like a normal
>> response except that it is not (str.find returing -1).
>> Python as distributed usually chooses 1b or 2.
>> I believe str.find and
>> .rfind are unique in the choice of 1a.
> That is not true. str.find's choice is not 1a,
It it the paradigm example of 1a as I meant my definition.
> -1 does *not* look like a normal response,
> since a normal response is non-negative.
Actually, the current doc does not clearly specify to some people that the
response is a count. That is what lead to the 'str.find is buggy' thread
on c.l.p, and something I will clarify when I propose a doc patch.
In any case, Python does not have a count type, though I sometime wish it
did. The return type is int and -1 is int, though it is not meant to be
used as an int and it is a bug to do so.
>It is *not* the only method with choice 1a):
> dict.get returns None if the key is not found,
None is only the default default, and whatever the default is, it is not
necessarily an error return. A dict accessed via .get can be regarded as
an infinite association matching all but a finite set of keys with the
default. Example: a doubly infinite array of numbers with only a finite
number of non-zero entries, implemented as a dict. This is the view
actually used if one does normal calculations with that default return.
There is no need, at least for that access method, for any key to be
explicitly associated with the default.
If the default *is* regarded as an error indicator, and is only used to
guard normal processing of the value returned, then that default must not
be associated any key. There is the problem that the domain of dict
values is normally considered to be any Python object and functions can
only return Python objects and not any non-Python-object error return.
So the effective value domain for the particular dict must be the set
'Python objects' minus the error indicator. With discipline, None often
works. Or, to guarantee 1b-ness, one can create a new type that cannot be
in the dict.
> For another example, file.read() returns an empty string at EOF.
If the request is 'give me the rest of the file as a string', then '' is
the answer, not a 'cannot answer' indicator. Similarly, if the request is
'how many bytes are left to read', then zero is a numerical answer, not a
non-numerical 'cannot answer' indicator.
Terry J. Reedy
More information about the Python-Dev