strange side effect with lists!?

Duncan Booth duncan.booth at invalid.invalid
Wed Oct 13 09:13:07 EDT 2004


Bengt Richter wrote:

> I got curious:
> 
> >>> L = range(5)
> >>> L
>  [0, 1, 2, 3, 4]
> 
<various manipulations of a recursive data structure snipped>
 
> I'm impressed ;-)
> 

I find it impressive that recursive data structures involving builtin 
types manage to avoid printing out infinite regressions of themselves.

>>> class C:
    def __init__(self, val):
        self.val = val
    def __repr__(self):
        return "<C %s>" % self.val

>>> l = range(5)
>>> l
[0, 1, 2, 3, 4]
>>> c = C(l)
>>> c
<C [0, 1, 2, 3, 4]>
>>> l[2] = c
>>> l
[0, 1, <C [...]>, 3, 4]
>>> c
<C [0, 1, <C [...]>, 3, 4]>





More information about the Python-list mailing list