[Python-Dev] partition() (was: Remove str.find in 3.0?)

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Tue Aug 30 11:02:11 CEST 2005



Josiah Carlson a écrit :
> Pierre Barbier de Reuille <pierre.barbier at cirad.fr> wrote:
> 
>>Well, I want to come back on a point that wasn't discussed. I only found
>>one positive comment here :
>>http://mail.python.org/pipermail/python-dev/2005-August/055775.html
> 
> 
> You apparently haven't been reading python-dev for around 36 hours,
> because there have been over a dozen positive comments in regards to
> str.partition().

Well, I wasn't criticizing the overall idea of str.partition, which I
found very useful ! I'm just discussing one particular idea, which is to
avoid the use of exceptions.

> 
>>Raymond Hettinger wrote:
>>
>>>* The function always succeeds unless the separator argument is not a
>>>string type or is an empty string.  So, a typical call doesn't have to
>>>be wrapped in a try-suite for normal usage.
>>
>>Well, I wonder if it's so good ! Almost all the use case I find would
>>require something like:
>>
>>head, sep, tail = s.partition(t)
>>if sep:
>>   do something
>>else:
>>   do something else
> 
> 
> Why don't you pause for a second and read Raymond's post here:
> http://mail.python.org/pipermail/python-dev/2005-August/055781.html
> 
> In that email there is a listing of standard library translations from
> str.find to str.partition, and in every case, it is improved.  If you
> believe that str.index would be better used, take a moment and do a few
> translations of the sections provided and compare them with the
> str.partition examples.


Well, what it does is exactly what I tought, you can express most of the
use-cases of partition with:

head, sep, tail = s.partition(sep)
if not sep:
  #do something when it does not work
else:
  #do something when it works

And I propose to replace it by :

try:
  head, sep, tail = s.partition(sep)
  # do something when it works
except SeparatorError:
  # do something when it does not work

What I'm talking about is consistency. In most cases in Python, or at
least AFAIU, error testing is avoided and exception launching is
preferred mainly for efficiency reasons. So my question remains: why
prefer for that specific method returning an "error" value (i.e. an
empty separator) against an exception ?

Pierre

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Python-Dev mailing list