8

Python の優れた点の 1 つは、メソッドと関数をイントロスペクションできることです。例として、関数の署名を取得するに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.

そしてx、オプションbaseで がこの関数のパラメータであることを確認してください。

新しい gtk3 と自動で生成された pygobject bindings(*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>

ここでの質問: これ (Python バインディングのメソッド イントロスペクションの喪失) は、もう一度変更されるもの (ドキュメント) の努力が pygobjects に費やされたのか、それとも pygobjects の動作方法のためにここに残っているものなのか?

4

3 に答える 3

4

Right now, I think this isn't yet ready. However, you can still do manual introspection looking at Gtk-3.0.gir file (in my system located in /usr/share/gir-1.0/Gtk-3.0.gir).

The gir file is just an xml file that is supposed to be used exactly to explore the exposed interface regardless of the programming language that you are using. For example, the Label class can be found looking for <class name="Label". Inside the class tag there's a doc tag with extensive information about what this widget is supposed to do. Also there are many method tags and one of them is the one you're interested in in you example: <method name="set_text". Inside this method tag there's not only a doc tag that describes the method, but also a parameters tag that, in turn, contains some parameter tag that are used to describe each parameter in terms of name, description and type:

<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>

So all the information is already there.

于 2011-11-10T12:00:53.657 に答える
0

dir()、vars() などの別の組み込み関数を使用できます。

http://docs.python.org/library/functions.html

別の便利なツールは pydoc です。

pydoc gi.repository.Gtk
于 2011-11-01T12:05:45.363 に答える