[Python-3000] Possible Duck Typing Problem in Python 2.5?

Guilherme Polo ggpolo at gmail.com
Sun Dec 9 16:45:14 EST 2007


2007/12/9, hashcollision <hashcollision at gmail.com>:
> 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 withTypeError: can only concatenate list (not "instance") to list
> I tried:
> class X(list):
>  internal = [5, 6, 7, 8]
>
>  def __getitem__(self, i):
>   return self.internal[i]
>  def __len__(self):
>   return internal
>  def __iter__(self):
>   return internal.__iter__()
> but this fails also.

Try this:

class X(list):
  internal = [5, 6, 7, 8]
  def __init__(self):
    list.__init__(self, self.internal)

x = X()
l = [1,2,3]
print l + x

> IMHO, this is a problem. Is it? If so, I suggest that it be fixed in python
> 3000.
>
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe:
> http://mail.python.org/mailman/options/python-3000/ggpolo%40gmail.com
>
>


-- 
-- Guilherme H. Polo Goncalves



More information about the Python-list mailing list