[Python-3000] Possible Duck Typing Problem in Python 2.5?
Daniel Stutzbach
daniel at stutzbachenterprises.com
Mon Dec 10 00:03:33 CET 2007
On Dec 9, 2007 3:21 PM, hashcollision <hashcollision at gmail.com> wrote:
> From http://ivory.idyll.org/blog/dec-07/conversions.html:
>
> class X:
> internal = [5,6,7,8]
> def __getitem__(self, i):
> return self.internal[i]
>
> x = X()
>
> l = [1,2,3]
> print l + x
>
> fails with
>
> TypeError: can only concatenate list (not "instance") to list
>
> If you want class X to understand "+" via duck-typing, overload __add__
and __radd__.
--
Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises LLC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20071209/5a1722bf/attachment.htm
More information about the Python-3000
mailing list