4

Given a class such as

class MyClass:
    text = "hello"
    number = 123

Is there a way in python to inspect MyClass an determine that it has the two attributes text and number. I can not use something like inspect.getSource(object) because the class I am to get it's attributes for are generate using SWIG (so they are hidden in .so :) ).

So I am really looking for something equivalant to Java's [Class.getDeclardFields][1]

Any help would be appreciated, otherwise I'll have to solve this problem with SWIG + JAVA instead of SWIG + Python.

hhafez
  • 38,949
  • 39
  • 113
  • 143
  • Since there are no "declared" fields, there's really nothing quite like Java's `getDeclardFields`. If you could explain why you think you need to know the attributes, it might help to answer the question. – S.Lott Apr 10 '10 at 11:43
  • The question has kind of been answered, but essentially I have a large collection of C modules that parse binary files and read them into structs. I wanted to be able to reflectivly print out the contents of each struct in a nice generic way without having to right new code for each module as they change often (new fields, change in sizes etc) and we add more often. Using SWIG, I can automaticly generated the required .i files and using python I can genericly handle the display of any struct – hhafez Apr 11 '10 at 01:16
  • Please do not put important information in a comment. Please UPDATE your question to include ALL the information. Then delete the comment. Once we see what your real problem is, we might be able to help. – S.Lott Apr 12 '10 at 13:29
  • Thanks for your concern, however given the fact the question was answered correctly several times before I posted my comment, I believe I didn't "put important information in a comment", Infact I believe sufficient info to answer correctly (just look at how quick the answers came after I asked :) ). – hhafez Apr 12 '10 at 22:33

3 Answers3

8

I usually just use dir(MyClass). Works on instantiated objects too.

edit: I should mention this is a shorthand function I use for figuring out if my objects are getting created correctly. You might want to look more carefully into the reflection API's if you're doing this programmatically.

Also it may not work on linked libraries.

Garrett Bluma
  • 1,312
  • 10
  • 14
0
>>> import cmath
>>> dir(cmath)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'isinf', 'isnan', 'log', 'log10', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
>>> cmath.atan
<built-in function atan>

is dirable and

open("/usr/lib/python2.6/lib-dynload/cmath.so", O_RDONLY) = 4
read(4, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0@\17\0\0004\0\0\0"..., 512) = 512
fstat64(4, {st_mode=S_IFREG|0644, st_size=32176, ...}) = 0
mmap2(NULL, 43824, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 4, 0) = 0x268000
mmap2(0x26f000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 4, 0x6) = 0x26f000
mmap2(0x271000, 6960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x271000
close(4)

is dynamically loaded

msw
  • 42,753
  • 9
  • 87
  • 112
-1

Please write actual, executable code snippets; don't expect people answering your question to first fix your code.

class MyClass(object):
    text = "hello"
    number = 123

for a in dir(MyClass):
    print a
Glenn Maynard
  • 55,829
  • 10
  • 121
  • 131