print object attributes recursively

L

lallous

Hello

Suppose I have this code:

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

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

t = Y(5)


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

Thanks,
Elias
 
P

Piet van Oostrum

lallous said:
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.
 
T

TerryP

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.

PHP's print_r() is basically a recursive pretty printer; if you know
Perl, think of it as PHPs idea of Data::Dumper.


Personally, when I want to know whats in something, I invoke dir() on
it and filter the results accordingly. Whether it is smart or stupid,
it works well at an interactive prompt.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top