Unexpected behavior of list of list

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 9 22:52:16 EST 2007


En Fri, 09 Mar 2007 23:58:59 -0300, Steven D'Aprano  
<steve at REMOVE.THIS.cybersource.com.au> escribió:

> You know, now that Python has a warnings module, it would be really good
> if list-of-lists*int raised a warning. Does anyone know if that's  
> feasible
> or possible? It needn't catch every imaginable Gotcha, just the common
> case. I'm thinking something like this:
>
> # pseudo-code
> class list:
>     def __mul__(self, count):
>         for item in self:
>             if isinstance(item, list):
>                 warn("this duplicates references, not copies")
>                 break
>         ...do the rest

But isn't the same with almost any other object (except inmutable ones)?
[{}]*5, [set()]*5, even [my_instance]*5?
It's hard to tell when it's a legitimate use and when it's a mistake, and  
the checking+warning call has a cost...

-- 
Gabriel Genellina




More information about the Python-list mailing list