print object attributes recursively

Piet van Oostrum piet at cs.uu.nl
Wed Sep 30 09:49:47 EDT 2009


>>>>> "lallous" <lallous at lgwm.org> (l) wrote:

>l> Hello
>l> Suppose I have this code:

>l> class X:
>l>    def __init__(self, n):
>l>        self.L = [x for x in xrange(0, n+1)]

>l> class Y:
>l>    def __init__(self, n):
>l>        self.M = [X(x) for x in xrange(0, n)]

>l> t = Y(5)


>l> How can I easily print "t" and all its nested attributes? (Something like
>l> PHP's print_r())

I don't know what print_r does, but in your example above

print [x.L for x in t.M] would work.

Probably you would split this into two methods in X and Y.
-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list