0

そのモジュール内に動的に作成されたクラスを持つpythonモジュールを動的に作成しています。ただし、Python でヘルプ機能を使用すると、クラスが表示されません。問題の例を次に示します。

import imp
Foo = imp.new_module("Foo")
Foo.Bar = type('Bar', (object,), dict(x=10, y=20))
help(Foo)

これは次のことを示しています。

Help on module Foo:

NAME
    Foo

FILE
    (built-in)

CLASSESセクションに Bar を表示してほしいです。それ、どうやったら出来るの?

help(Foo.Bar)クラスを として記述していることに注意してくださいin module __main__。それは手がかりですか?

Help on class Bar in module __main__:

class Bar(__builtin__.object)
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  x = 10
 |  
 |  y = 20
4

2 に答える 2

2

__module__の設定Bar:

Foo.Bar.__module__ = Foo.__name__

そして現れます。属性に従って、モジュールの一部ではないhelp()すべてを除外します。残りは海外輸入と想定。.__module__

于 2013-04-19T16:21:53.397 に答える