Movatterモバイル変換
[0]ホーム
[Python-Dev] partition() (was: Remove str.find in 3.0?)
Pierre Barbier de Reuillepierre.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 Ifound very useful ! I'm just discussing one particular idea, which is toavoid 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 theuse-cases of partition with:head, sep, tail = s.partition(sep)if not sep: #do something when it does not workelse: #do something when it worksAnd I propose to replace it by :try: head, sep, tail = s.partition(sep) # do something when it worksexcept SeparatorError: # do something when it does not workWhat I'm talking about is consistency. In most cases in Python, or atleast AFAIU, error testing is avoided and exception launching ispreferred mainly for efficiency reasons. So my question remains: whyprefer for that specific method returning an "error" value (i.e. anempty separator) against an exception ?Pierre-- Pierre Barbier de ReuilleINRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAPBotanique et Bio-informatique de l'Architecture des PlantesTA40/PSII, Boulevard de la Lironde34398 MONTPELLIER CEDEX 5, Francetel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
More information about the Python-Devmailing list
[8]ページ先頭