views:

56

answers:

1

If I had:

class A(object):
    varA = 1

inst = A()

Then how would I retrieve the keys of all variables on inst? I'd want something like ["varA"]

So far, I've gotten this:

vars(inst.__class__).keys() #returns ['__dict__', '__weakref__', '__module__', 'varA', '__doc__']

I'm fine with that, I'd just ignore the double-under vars. My problem is with multiple layers of inheritance like:

class A(object):
    varA = 1

class B(A):
    varB = 2

inst = B()
vars(inst.__class__).keys() #returns ['__module__', '__doc__', 'varB']

but I want to retrieve both varB and varA. Any idea how I would go about doing this?

I also tried:

vars(super(B, inst).__class__).keys()+vars(inst.__class__).keys()

But that didn't do what I expected.

If it matters, I'm using Python 2.6.

Edit: I actually just stumbled across a very easy way to do this: dir(inst)

+1  A: 

There is a python module, called inspect for runtime introspection. Maybe inspect.getmembers can help you ...

The MYYN
Yep, that's exactly what I wanted. I've used inspect in the past, but I had forgotten what it's capable of.
Wallacoloo