[Python-ideas] Fast sum() for non-numbers

Sergey sergemp at mail.ru
Fri Jul 19 04:36:08 CEST 2013


On Jul 18, 2013 Terry Reedy wrote:
>>>> [1] http://bugs.python.org/file30917/fasttuple.py
>>>>>> I do not like that implementation, because it shares the underlying
>>> storage. This means that tuples which ought to be small will grow and
>>> grow and grow just because you have called __add__ on a different tuple.
>>>>>> Using Python 2.7 and your implementation above:
>>>>>> py> a = ft([]) # empty tuple
>>> py> len(a._store)
>>> 0
>>> py> b = ft([1])
>>> py> c = a + b
>>> py> d = ft([2]*10000)
>>> py> c = c + d
>>> py> len(a._store)
>>> 10001
>>>>>> So adding a big tuple to c changes the internal storage of a.
>>>> Yes, that's right. All 3 variables `a`, `b` and `c` share the same
>> storage, so you effectively get 3 variables for the price of one. :)
>> That's the concept. Why is that bad?
>> What happens to len(a._store) after del c?

In that proof-of-concept implementation? Nothing. I tried to keep
it simple, so that the idea was easier to understand.
Its technically possible to have __del__ resizing internal store,
but is it really needed?
-- 


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /