[Python-ideas] Fast sum() for non-numbers
Terry Reedy
tjreedy at udel.edu
Fri Jul 5 09:55:55 CEST 2013
On 7/5/2013 2:43 AM, Sergey wrote:
> I don't like the idea that `a` implicitly changes when I change `b`.
When a and b are the same things or one is part of the other, that must
happen. This is the nature of mutable structures (when all or part of a
structure can be named, which should be always). Here is Andrew
Barnett's example translated into Python.
a = [1, [2, [3, None]]]
b = a[1]
b[1] = [10, [20, None]]
print(a)
>>>
[1, [2, [10, [20, None]]]]
The difference bettween that being a lisp list and a python list is that
list sees len(a) == 4, which Python would say 2, but that is because
lisp looks as simple nested structures as if there were flat, becuase
the nesting is, in a sense, an internal detail. Here is a possible
corresponding iadd.
class Lisp(list):
# the second member of each Lisp is either a Lisp or None.
def __iadd__(self, other):
if not instance(other, Lisp):
raise TypeError
while self[1] is not None:
self = self[1]
self[1] = other
--
Terry Jan Reedy
More information about the Python-ideas
mailing list