Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20788

Re: sum() requires number, not simply __add__

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'implements': 0.05; 'type,': 0.07; 'python': 0.08; '(self,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; "'a'": 0.16; "'int'": 0.16; 'believe.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:() ': 0.16; 'subject:sum': 0.16; 'language': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'subject:not': 0.19; '(most': 0.21; "doesn't": 0.22; 'from:addr:web.de': 0.23; 'operand': 0.23; 'traceback': 0.24; 'fix': 0.25; 'expect': 0.25; 'not.': 0.28; 'pass': 0.29; 'explicitly': 0.29; 'class': 0.29; 'null)': 0.30; 'subject:number': 0.30; 'typeerror:': 0.30; 'error': 0.30; 'implement': 0.32; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'certain': 0.34; 'last):': 0.34; 'null': 0.34; 'to:addr:python- list': 0.35; 'two': 0.36; 'received:org': 0.36; 'but': 0.37; 'skip:_ 10': 0.38; 'could': 0.38; 'should': 0.38; "i'd": 0.39; 'to:addr:python.org': 0.40; 'type': 0.61; 'design': 0.61; 'buck': 0.84; 'other):': 0.84; 'self.': 0.84; 'type(s)': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: sum() requires number, not simply __add__
Date Fri, 24 Feb 2012 09:29:20 +0100
Organization None
References <91c71e41-b98c-46f6-b3af-bed894cf9d92@kh11g2000pbb.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849ddf.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.114.1330072130.3037.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330072130 news.xs4all.nl 6931 [2001:888:2000:d::a6]:33860
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20788

Show key headers only | View raw


Buck Golemon wrote:

> I feel like the design of sum() is inconsistent with other language
> features of python. Often python doesn't require a specific type, only
> that the type implement certain methods.
> 
> Given a class that implements __add__ why should sum() not be able to
> operate on that class?
> 
> We can fix this in a backward-compatible way, I believe.
> 
> Demonstration:
>     I'd expect these two error messages to be identical, but they are
> not.
> 
>      >>> class C(object): pass
>      >>> c = C()
>      >>> sum((c,c))
>     TypeError: unsupported operand type(s) for +: 'int' and 'C'
>     >>> c + c
>     TypeError: unsupported operand type(s) for +: 'C' and 'C'

You could explicitly provide a null object:

>>> class Null(object):
...     def __add__(self, other):
...             return other
...
>>> null = Null()
>>> class A(object):
...     def __init__(self, v):
...             self.v = v
...     def __add__(self, other):
...             return A("%s+%s" % (self, other))
...     def __str__(self):
...             return self.v
...     def __repr__(self):
...             return "A(%r)" % self. v
...
>>> sum(map(A, "abc"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'A'
>>> sum(map(A, "abc"), null)
A('a+b+c')

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:19 -0800
  Re: sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:23 -0800
    Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:41 +0000
    Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 08:53 +1100
      Re: sum() requires number, not simply __add__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-23 23:33 +0000
        Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 10:39 +1100
        Re: sum() requires number, not simply __add__ Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2012-02-24 12:41 +0100
          Re: sum() requires number, not simply __add__ Roy Smith <roy@panix.com> - 2012-02-24 08:23 -0500
            Re: sum() requires number, not simply __add__ Terry Reedy <tjreedy@udel.edu> - 2012-02-24 16:58 -0500
    Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:59 +0000
    Re: sum() requires number, not simply __add__ Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-23 15:00 -0700
    Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 09:04 +1100
    Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 22:09 +0000
  Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:32 +0000
  Re: sum() requires number, not simply __add__ Chris Rebert <clp2@rebertia.com> - 2012-02-23 13:32 -0800
    Re: sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:38 -0800
      Re: sum() requires number, not simply __add__ Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-23 14:54 -0700
  Re: sum() requires number, not simply __add__ Stefan Behnel <stefan_ml@behnel.de> - 2012-02-23 22:42 +0100
    Re: sum() requires number, not simply __add__ Duncan Booth <duncan.booth@invalid.invalid> - 2012-02-24 11:40 +0000
  Re: sum() requires number, not simply __add__ Peter Otten <__peter__@web.de> - 2012-02-24 09:29 +0100

csiph-web