Slicing vs .startswith
Batista, Facundo
FBatista at uniFON.com.ar
Mon Sep 22 15:14:58 EDT 2003
Sorry for not replying the original message.
See PEP 08:
- Avoid slicing strings when checking for prefixes or suffixes.
Use startswith() and endswith() instead, since they are faster,
cleaner and less error prone. E.g.:
No: if foo[:3] == 'bar':
Yes: if foo.startswith('bar'):
The exception is if your code must work with Python 1.5.2 (but
let's hope not!).
. Facundo
#- -----Mensaje original-----
#- De: David Eppstein [mailto:eppstein at ics.uci.edu]
#- Enviado el: Lunes 22 de Septiembre de 2003 3:52 PM
#- Para: python-list at python.org
#- Asunto: Re: Slicing vs .startswith
#-
#-
#- In article <3F6F3E38.5221D7E7 at engcorp.com>,
#- Peter Hansen <peter at engcorp.com> wrote:
#-
#- > > For example, which one of the following would be more
#- efficient, or ,
#- > > moreover, more pythonic?
#- > >
#- > > if aa[:3] == 'abc':
#- > >
#- > > vs
#- > >
#- > > if aa.startswith('abc'):
#- >
#- > The latter is clearly more readable.
#-
#- More Pythonic, too, I think. "Readability counts," and
#- "There should be
#- one-- and preferably only one --obvious way to do it." In
#- this case,
#- startswith must be the one obvious way, or else why would it
#- exist in
#- the standard library at all?
#-
#- --
#- David Eppstein http://www.ics.uci.edu/~eppstein/
#- Univ. of California, Irvine, School of Information & Computer Science
#- --
#- http://mail.python.org/mailman/listinfo/python-list
#-
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
ADVERTENCIA
La información contenida en este mensaje y cualquier archivo anexo al mismo,
son para uso exclusivo del destinatario y pueden contener información
confidencial o propietaria, cuya divulgación es sancionada por la ley.
Si Ud. No es uno de los destinatarios consignados o la persona responsable
de hacer llegar este mensaje a los destinatarios consignados, no está
autorizado a divulgar, copiar, distribuir o retener información (o parte de
ella) contenida en este mensaje. Por favor notifíquenos respondiendo al
remitente, borre el mensaje original y borre las copias (impresas o grabadas
en cualquier medio magnético) que pueda haber realizado del mismo.
Todas las opiniones contenidas en este mail son propias del autor del
mensaje y no necesariamente coinciden con las de Telefónica Comunicaciones
Personales S.A. o alguna empresa asociada.
Los mensajes electrónicos pueden ser alterados, motivo por el cual
Telefónica Comunicaciones Personales S.A. no aceptará ninguna obligación
cualquiera sea el resultante de este mensaje.
Muchas Gracias.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030922/666a88fd/attachment.html>
More information about the Python-list
mailing list