Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29499
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeeder.ewetel.de!eweka.nl!lightspeed.eweka.nl!69.16.177.246.MISMATCH!cyclone03.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!npeersf03.am4!fx18.am4.POSTED!not-for-mail |
|---|---|
| From | Alister <alister.ware@ntlworld.com> |
| Subject | Re: sum works in sequences (Python 3) |
| Newsgroups | comp.lang.python |
| References | <franck-E8B1EB.16412019092012@news.free.fr> |
| User-Agent | Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| Lines | 39 |
| Message-ID | <scl6s.509699$Ax.328580@fx18.am4> (permalink) |
| NNTP-Posting-Host | 82.18.78.130 |
| X-Complaints-To | http://netreport.virginmedia.com |
| X-Trace | 1348067224 82.18.78.130 (Wed, 19 Sep 2012 15:07:04 UTC) |
| NNTP-Posting-Date | Wed, 19 Sep 2012 15:07:04 UTC |
| Organization | virginmedia.com |
| Date | Wed, 19 Sep 2012 15:07:04 GMT |
| X-Received-Bytes | 1782 |
| Xref | csiph.com comp.lang.python:29499 |
Show key headers only | View raw
On Wed, 19 Sep 2012 16:41:20 +0200, Franck Ditter wrote:
> Hello,
> I wonder why sum does not work on the string sequence in Python 3 :
>
>>>> sum((8,5,9,3))
> 25
>>>> sum([5,8,3,9,2])
> 27
>>>> sum('rtarze')
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>
> I naively thought that sum('abc') would expand to 'a'+'b'+'c'
> And the error message is somewhat cryptic...
>
> franck
Summation is a mathematical function that works on numbers
Concatenation is the process of appending 1 string to another
although they are not related to each other they do share the same
operator(+) which is the cause of confusion.
attempting to duck type this function would cause ambiguity for example
what would you expect from
sum ('a','b',3,4)
'ab34' or 'ab7' ?
even 'A' + 7 would return this error for same reason.
--
It is the nature of extreme self-lovers, as they will set an house on
fire,
and it were but to roast their eggs.
-- Francis Bacon
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sum works in sequences (Python 3) Franck Ditter <franck@ditter.org> - 2012-09-19 16:41 +0200
Re: sum works in sequences (Python 3) Joel Goldstick <joel.goldstick@gmail.com> - 2012-09-19 10:57 -0400
Re: sum works in sequences (Python 3) Neil Cerutti <neilc@norwich.edu> - 2012-09-19 14:57 +0000
Re: sum works in sequences (Python 3) Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-19 09:03 -0600
Re: sum works in sequences (Python 3) Neil Cerutti <neilc@norwich.edu> - 2012-09-19 15:06 +0000
Re: sum works in sequences (Python 3) Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-19 09:24 -0600
Re: sum works in sequences (Python 3) Steve Howell <showell30@yahoo.com> - 2012-09-19 08:37 -0700
Re: sum works in sequences (Python 3) Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-19 12:33 -0600
Re: sum works in sequences (Python 3) Steve Howell <showell30@yahoo.com> - 2012-09-19 11:43 -0700
Re: sum works in sequences (Python 3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-19 16:14 +0000
Re: sum works in sequences (Python 3) Alister <alister.ware@ntlworld.com> - 2012-09-19 15:07 +0000
Re: sum works in sequences (Python 3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-19 16:18 +0000
Re: sum works in sequences (Python 3) Terry Reedy <tjreedy@udel.edu> - 2012-09-19 14:49 -0400
Re: sum works in sequences (Python 3) Hans Mulder <hansmu@xs4all.nl> - 2012-09-20 00:25 +0200
csiph-web