Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66232
| References | <4cc09129-43ee-4205-a24c-03f92b594abc@googlegroups.com> <ldj59s$iip$1@ger.gmane.org> <52FD1B01.80200@stoneleaf.us> <ldj677$oos$2@ger.gmane.org> <ldj6jo$iip$2@ger.gmane.org> |
|---|---|
| Date | 2014-02-14 07:29 +1100 |
| Subject | Re: A curious bit of code... |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.6875.1392323389.18130.python-list@python.org> (permalink) |
On Fri, Feb 14, 2014 at 6:32 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
>> There will be an exception only if it is zero-length. But good
>> point! That's a pretty sneaky way to avoid checking for a
>> zero-length string. Is it a popular idiom?
>>
>
> I hope not.
The use of slicing rather than indexing to avoid problems when the
string's too short? I don't know about popular, but I've certainly
used it a good bit. For the specific case of string comparisons you
can use startswith/endswith, but slicing works with other types as
well.
Also worth noting:
Python 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> s1,s2=b"asdf",u"asdf"
>>> s1[:1],s2[:1]
('a', u'a')
>>> s1[0],s2[0]
('a', u'a')
Python 3.4.0b2 (v3.4.0b2:ba32913eb13e, Jan 5 2014, 16:23:43) [MSC
v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> s1,s2=b"asdf",u"asdf"
>>> s1[:1],s2[:1]
(b'a', 'a')
>>> s1[0],s2[0]
(97, 'a')
When you slice, you get back the same type as you started with. (Also
true of lists, tuples, and probably everything else that can be
sliced.) When you index, you might not; strings are a special case
(since Python lacks a "character" type), and if your code has to run
on Py2 and Py3, byte strings stop being that special case in Py3. So
if you're working with a byte string, it might be worth slicing rather
than indexing. (Though you can still use startswith/endswith, if they
suit your purpose.)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A curious bit of code... forman.simon@gmail.com - 2014-02-13 10:37 -0800
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 13:45 -0500
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 10:45 -0800
Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 19:09 +0000
Re: A curious bit of code... Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-02-13 20:05 +0100
Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:17 +0000
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:20 -0800
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 14:28 -0500
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:25 -0800
Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:25 +0000
Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 19:32 +0000
Re: A curious bit of code... Peter Otten <__peter__@web.de> - 2014-02-13 20:43 +0100
Re: A curious bit of code... Marko Rauhamaa <marko@pacujo.net> - 2014-02-13 21:56 +0200
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:23 -0800
Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:51 +0000
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:59 -0800
Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 13:59 -0600
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 07:29 +1100
Re: A curious bit of code... Tim Chase <python.list@tim.thechases.com> - 2014-02-13 14:39 -0600
Re: A curious bit of code... Emile van Sebille <emile@fenx.com> - 2014-02-13 12:55 -0800
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:24 -0500
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 16:23 -0800
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:01 +1100
Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 21:01 +0000
Re: A curious bit of code... Peter Otten <__peter__@web.de> - 2014-02-13 22:06 +0100
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:10 +1100
Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 21:14 +0000
Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:20 -0600
Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:19 -0600
Re: A curious bit of code... Emile van Sebille <emile@fenx.com> - 2014-02-13 13:23 -0800
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:31 +1100
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:38 -0500
Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:47 -0600
Re: A curious bit of code... Serhiy Storchaka <storchaka@gmail.com> - 2014-02-13 23:49 +0200
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:51 -0500
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 13:33 -0800
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 09:13 +1100
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 14:26 -0800
Re: A curious bit of code... Terry Reedy <tjreedy@udel.edu> - 2014-02-13 19:29 -0500
Re: A curious bit of code... forman.simon@gmail.com - 2014-02-13 18:45 -0800
Re: A curious bit of code... Ned Batchelder <ned@nedbatchelder.com> - 2014-02-13 22:26 -0500
Re: A curious bit of code... forman.simon@gmail.com - 2014-02-14 12:04 -0800
Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-14 21:01 +0000
Re: A curious bit of code... Dave Angel <davea@davea.name> - 2014-02-14 07:19 -0500
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-14 09:06 -0500
csiph-web