您可以使用 this 检索对象的所有属性
dir(YouObjectInstance.props)
YourObjectInstance 当然是你创建的任何实例。
简单的方法可能是打开一个终端:
you@yourcomputer ~/Desktop/python $ python
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import Gtk, GtkSource, GObject
>>> window_instance = Gtk.Window()
>>> dir(window_instance.props)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__len__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'accept_focus', 'app_paintable', 'application', 'border_width', 'can_default', 'can_focus', 'child', 'composite_child', 'decorated', 'default_height', 'default_width', 'deletable', 'destroy_with_parent', 'double_buffered', 'events', 'expand', 'focus_on_map', 'focus_visible', 'gravity', 'halign', 'has_default', 'has_focus', 'has_resize_grip', 'has_tooltip', 'has_toplevel_focus', 'height_request', 'hexpand', 'hexpand_set', 'icon', 'icon_name', 'is_active', 'is_focus', 'margin', 'margin_bottom', 'margin_left', 'margin_right', 'margin_top', 'mnemonics_visible', 'modal', 'name', 'no_show_all', 'opacity', 'parent', 'receives_default', 'resizable', 'resize_grip_visible', 'resize_mode', 'role', 'screen', 'sensitive', 'skip_pager_hint', 'skip_taskbar_hint', 'startup_id', 'style', 'title', 'tooltip_markup', 'tooltip_text', 'transient_for', 'type', 'type_hint', 'ubuntu_no_proxy', 'urgency_hint', 'valign', 'vexpand', 'vexpand_set', 'visible', 'width_request', 'window', 'window_position']
>>>
现在您可以即时记录对象的属性。
如果你需要这些方法?
for names in dir(window_instance):
attr = getattr(window_instance,names)
if callable(attr):
print names,':',attr.__doc__
如果你想反思,你可以去这个链接:reflection api
这将为您节省大量时间。它也可以被修改为接受任何对象或被继承。
您还可以使用:
帮助(SomeClassModuleOrFunction)
虽然来自 help() 的打印文本可能会受到限制,但使用 instance.props 并在实例上循环也可能有缺点,具体取决于代码的文档记录程度。
使用上述任何一种方法至少可以获得一些文档。如果一个不适合您需要的,请尝试另一个。