Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83177
| Path | csiph.com!usenet.pasdenom.info!news.franciliens.net!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <gherron@digipen.edu> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'c++,': 0.07; 'element': 0.07; 'indexing': 0.07; 'string': 0.09; "'a'": 0.09; '(instead': 0.09; '*is*': 0.09; 'false.': 0.09; 'inclusion': 0.09; 'strings.': 0.09; 'subject:set': 0.09; 'works.': 0.09; 'python': 0.11; "'b'": 0.16; '(small)': 0.16; 'expected,': 0.16; 'happened?': 0.16; 'infinitely': 0.16; 'objects.': 0.16; 'surprising': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'patrick': 0.19; "python's": 0.19; 'result.': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'char': 0.24; 'string,': 0.24; 'test.': 0.24; '---': 0.24; "i've": 0.25; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'am,': 0.29; 'character': 0.29; 'characters': 0.30; 'skip:( 20': 0.30; "skip:' 10": 0.31; 'gary': 0.31; 'allows': 0.31; 'test': 0.35; 'but': 0.35; 'really': 0.36; 'explains': 0.36; 'false': 0.36; 'returning': 0.36; 'sequence': 0.36; 'should': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'fact': 0.38; 'planning': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'today': 0.64; 'different': 0.65; 'charset:windows-1252': 0.65; 'wish': 0.70; 'institute': 0.72; '4th': 0.74; 'surprise': 0.74; 'received:204': 0.75; 'dr.': 0.77; '2014,': 0.84; 'avast': 0.84; 'characters,': 0.84; 'different.': 0.84; 'fourth': 0.84 |
| Date | Sat, 03 Jan 2015 12:05:49 -0800 |
| From | Gary Herron <gherron@digipen.edu> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: surprise - byte in set |
| References | <54a83a19$0$6953$e4fe514c@dreader36.news.xs4all.nl> |
| In-Reply-To | <54a83a19$0$6953$e4fe514c@dreader36.news.xs4all.nl> |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17357.1420316107.18130.python-list@python.org> (permalink) |
| Lines | 89 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1420316107 news.xs4all.nl 2890 [2001:888:2000:d::a6]:51271 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:83177 |
Show key headers only | View raw
On 01/03/2015 10:50 AM, patrick vrijlandt wrote:
> Hello list,
>
> Let me first wish you all the best in 2015!
>
> Today I was trying to test for occurrence of a byte in a set ...
>
> >>> sys.version
> '3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit
> (Intel)]'
> >>> 'b' in 'abc'
> True
> >>> b'b' in b'abc'
> True
> >>> 'b' in set('abc')
> True
> >>> b'b' in set(b'abc')
> False
>
> I was surprised by the last result. What happened?
> (Examples simplified; I was planning to manipulate the set)
The surprise is really that the 3rd test is True not that the fourth is
False.
First, as should be expected, a byte string is a sequence of (small)
ints. So b'b' is a (short) byte string and the set set(b'abc') is
composed of three ints. You should not expect your inclusion test to
return True when testing for a bytes-type object in a set of int-type
objects. And that explains your False result in the 4th test.
>>> type(b'abc')
<class 'bytes'>
>>> type(b'abc'[0])
<class 'int'>
But things are different for strings. You might think a string is a
sequence of characters, but Python does not have a character type. In
fact the elements of a string are just 1 char long strings:
>>> type('abc')
<class 'str'>
>>> type('abc'[0])
<class 'str'>
You would not logically expect to find a string 'b' in a set of
characters in, say C++, where the two types are different. But that's
not the Python way. In Python a set of characters set('abc') is really
a set of (short) strings, and the character 'b' is really a (short)
string, so the inclusion test works.
Python's way of returning a 1-byte string when indexing a string
(instead of returning an element of type character) allows this
surprising result.
>>> 'abc'[0]
'a'
>>> 'abc'[0][0]
'a'
>>> 'abc'[0][0][0]
'a'
>>> 'abc'[0][0][0][0]
'a'
...
I've never considered this a problem, but a infinitely indexable object
*is* a bit of an oddity.
>
> Patrick
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast
> antivirussoftware.
> http://www.avast.com
>
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
surprise - byte in set patrick vrijlandt <pvrijlandt@gmail.com> - 2015-01-03 19:50 +0100 Re: surprise - byte in set Jason Friedman <jsf80238@gmail.com> - 2015-01-03 12:12 -0700 Re: surprise - byte in set Dan Stromberg <drsalists@gmail.com> - 2015-01-03 11:15 -0800 Re: surprise - byte in set Gary Herron <gherron@digipen.edu> - 2015-01-03 12:05 -0800 Re: surprise - byte in set patrick vrijlandt <pvrijlandt@gmail.com> - 2015-01-03 23:08 +0100 Re: surprise - byte in set Amir Arsalan <am1r.ar3alan@gmail.com> - 2015-01-03 22:38 +0330
csiph-web