overloading *something

Peter Otten __peter__ at web.de
Tue Nov 8 10:33:31 CET 2005


James Stroud wrote:

> I was attempting to re-define iter of a subclassed list, to find the
> "magic" method, but it didn't work. 

>>> class List(list):
...     def __iter__(self): return iter("abc")
...
>>> a = List([1,2,3])
>>> list(a)
['a', 'b', 'c']
>>> tuple(a)
(1, 2, 3)

list-to-tuple conversion is optimized for performance -- basically a
memcopy() of the internal data. As with a similar peculiarity with
file.write() and the print statement, the problem could be shunned if
python would check for the exact class instead of a test that is equivalent
to isinstance().

Peter




More information about the Python-list mailing list