1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| class toto(object):
def __init__(self, val=0, p=None):
self.val=val
self.p=p
def __iter__(self):
ret=self
while True:
yield ret
if ret.p == None: return
ret=ret.p
def __repr__(self):
return "%s=%d" % (hex(id(self)), self.val)
>>> a=toto(1)
>>> a
0x1214eb0=1
>>> b=toto(2, a)
>>> b
0x1214f70=2
>>> c=toto(3, b)
>>> c
0x1214dd0=3
>>> c.p
0x1214f70=2
>>> c.p.p
0x1214eb0=1
>>> b.p
0x1214eb0=1
>>> for x in c:
print x
0x1214dd0=3
0x1214f70=2
0x1214eb0=1
>>> [x for x in c]
[0x1214dd0=3, 0x1214f70=2, 0x1214eb0=1] |
Partager