tom arnall wrote:
Bruno Desthuilliers wrote:
>tom arnall a écrit :
>>does anyone know of a utility to do a recursive dump of object data
members?
What are "object data members" ? (hint: in Python, everything is an
object - even functions and methods).
What is your real use case ?
something like:
class A:
Make it :
class A(object):
def __init__(self, p1):
self.p1 = p1
class B:
def __init__(self,p1, p2):
self.a = A(p1)
self.p2 = p2
self.v1 = '3'
class C:
def __init__(self):
self.b = B(3,4)
self.p3 = 5
class D:
def __init__(self):
self.v2=2
self.o1 = C()
self.o2 = B(11,12)
d = D()
objectDataDumper(d)
would produce something like:
object of class D with:
o1(C)->b(B)->a(A)->p1=3
o1(C)->b(B)->p2=4
o1(C)->b(B)->v1=3
o1(C)->p3=5
o2(B)->a(A)->p1=11
o2(B)->p2=12
o2(B)->v1=3
v2=2
Ok, so this is for debugging purpose ?
A small question : how should this behave for:
- _implementation attributes
- __magic__ attributes
- class attributes
- callable attributes
- properties and other descriptors ?
In fact, the problem is that in Python, everything's an object, and an
object is mostly a composed namespace (ie a set of name-object mappings)
subject to some lookup rules. FWIW, the class of an object is an
attribute (__class__) referencing the class objet; superclasses are
class objects referenced by the __bases__ and __mro__ attributes of the
class object; method objects are usually attributes of the class object,
but it's possible to add/replace methods on a per-instance basis;
properties are class attributes that most of the time works on some
instance attribute. etc, etc, etc... Even the code of a function is an
attribute of the function object. And as an icing on top of the cake,
there's the __getattr__ magic method...
With all this in mind, writing a generic python object inspector is not
that trivial : either you'll end up with way too much informations or
you'll arbitrary skip informations that would be useful in a given
context or you'll need to pass so much args to the inspector that it'll
become too heavy for a quick, intuitive use... Well, that's at least my
own experience.
OTOH, "live" inspection of an object (either in the Python shell or in a
pdb session) works fine. The pprint and inspect modules may help too.
My 2 cents...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"