对pygtk3的反思可能吗?

xub*_*tix 8 python pygtk pygobject gtk3

python的一大优点是能够对方法和函数进行内省.举个例子,要获得函数签名,math.log你可以(在ipython中)运行:

In [1]: math.log?
Type:       builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:    <built-in function log>
Namespace:  Interactive
Docstring:
    log(x[, base])

    Return the logarithm of x to the given base.
    If the base not specified, returns the natural logarithm (base e) of x.
Run Code Online (Sandbox Code Playgroud)

并查看该功能的参数x和可选项base.

使用新的gtk3和自动生成的pygobject绑定,我可以在所有示例中尝试只获取(*args, **kwargs)每个gtk方法的参数.

示例:Label.set_text 需要一个字符串:

In [1]: from gi.repository import Gtk
In [2]: mylabel = Gtk.Label("hello")
In [3]: mylabel.set_text?
Type:       instancemethod
Base Class: <type 'instancemethod'>
String Form:    <bound method Label.set_text of <Label object at 0x275b230 (GtkLabel at 0x28cd040)>>
Namespace:  Interactive
File:       /usr/lib/python2.7/dist-packages/gi/types.py
Definition: L.set_text(*args, **kwargs)
Docstring:
    <no docstring>
Run Code Online (Sandbox Code Playgroud)

现在的问题:这是(丢失方法对python绑定的内省)会再次改变(文档)努力已经进入pygobjects或者这是由于pygobjects的工作方式而存在的东西吗?

jco*_*ado 4

现在,我认为这还没有准备好。但是,您仍然可以手动内省查看Gtk-3.0.gir文件(在我的系统中位于/usr/share/gir-1.0/Gtk-3.0.gir)。

gir 文件只是一个 xml 文件,无论您使用哪种编程语言,都应该准确地使用它来探索公开的接口。例如,Label可以通过寻找 来找到该类<class name="Label"。标签内部class有一个doc标签,其中包含有关此小部件应该执行的操作的大量信息。还有很多method标签,其中之一就是您在示例中感兴趣的标签:<method name="set_text"。在这个method标签内,不仅有一个doc描述该方法的标签,还有一个parameters标签,该标签又包含一些parameter用于描述每个参数的名称、描述和类型的标签:

<parameters>
  <parameter name="str" transfer-ownership="none">
    <doc xml:whitespace="preserve">The text you want to set</doc>
    <type name="utf8" c:type="gchar*"/>
  </parameter>
</parameters>
Run Code Online (Sandbox Code Playgroud)

所以所有的信息都已经在那里了。