views:

1813

answers:

5

I'm using Python 2.x and I have an object I'm summoning from the aether; the documentation on it is not particularly clear. I would like to be able to get a list of properties for that object and the type of each property.

Similarly, I'd like to get a list of methods for that object, as well, plus any other information I could find on that method, such as number of arguments and their respective types.

I have a feeling that I am simply missing the correct jargon in my Google searches. Not that I want to derail with specifics, but it's Active Directory, so that's always fun.

+6  A: 

Well ... Your first stop will be a simple dir(object). This will show you all the object's members, both fields and methods. Try it in an interactive Python shell, and play around a little.

For instance:

> class Foo:
   def __init__(self):
    self.a = "bar"
    self.b = 4711

> a=Foo()
> dir(a)
['__doc__', '__init__', '__module__', 'a', 'b']
unwind
I did try dir, but I didn't see some properties I know are available. It's an Active Directory object (ADsNameSpaces, GetObject), and I was hoping to see if the various attributes like "sn," "cn," and so forth were in the list, but they aren't. Perhaps I have selected the wrong object for this.
+3  A: 

How about something like:

>>> o=object()
>>> [(a,type(o.__getattribute__(a))) for a in dir(o)]
[('__class__', <type 'type'>), ('__delattr__', <type 'method-wrapper'>), 
('__doc__', <type 'str'>), ('__format__', <type 'builtin_function_or_method'>),
('__getattribute__', <type 'method-wrapper'>), ('__hash__', <type 'method-wrapper'>),
('__init__', <type 'method-wrapper'>), 
('__new__', <type 'builtin_function_or_method'>),
('__reduce__', <type 'builtin_function_or_method'>),
('__reduce_ex__', <type 'builtin_function_or_method'>),
('__repr__', <type 'method-wrapper'>), ('__setattr__', <type 'method-wrapper'>),
('__sizeof__', <type 'builtin_function_or_method'>),
('__str__', <type 'method-wrapper'>),
('__subclasshook__', <type 'builtin_function_or_method'>)]
>>>

A more structured method will be to use the inspect module:

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.

gimel
That's rather nasty with regards to the getattribute stuff. It's much better to use the getattr() built-in.
Devin Jeanpierre
feel free to replace o.__getattribute__(a) with getattr(o,a)
gimel
+1  A: 

Here's a nice article on Python introspection to get you started.

Akbar ibrahim
A: 

you could have a look at inspect module. it provides a wide variety of tools for inspection of live objects as well as source code.

SilentGhost
A: 

If you're using win32com.client.Dispatch, inspecting the python object might not be much help as it's a generic wrapper for IDispatch. You can use makepy (which comes with Activestate Python) to generate a python wrapper from the type library. Then you can look at the code for the wrapper.

ConcernedOfTunbridgeWells