Skip to content

Instantly share code, notes, and snippets.

@smarnach
Created June 12, 2012 19:49
Show Gist options
  • Save smarnach/2919731 to your computer and use it in GitHub Desktop.
Save smarnach/2919731 to your computer and use it in GitHub Desktop.
class A(object):
def __init__(self, i):
self.i = i
def __radd__(self, other):
return self.i
a = [A(42)]
# Test with built-in sum()
print(sum(a))
# prints 42
def sum(s, start=None):
it = iter(s)
try:
n= next(it)
except:
return 0
if start is None:
start = type(n)()
return n + __builtins__.sum(it, start)
# Test with proposed new version
print(sum(a))
# results in
# TypeError: __init__() takes exactly 2 arguments (1 given)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment