[英]How do I make the python help system show dynamically created classes in my dynamically created module?
我正在动态创建一个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)
我希望Bar出现在CLASSES部分中。 我怎么做?
请注意, 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
设置Bar
的__module__
:
Foo.Bar.__module__ = Foo.__name__
它会出现。 根据.__module__
属性, help()
过滤掉不属于模块的所有内容。 其余假定为外部进口。
如果将Foo.__all__
设置为包括'Bar'
,则Bar
将列在CLASSES
部分中:
import imp
Foo = imp.new_module('Foo')
Foo.Bar = type('Bar', (object,), dict(x=10, y=20))
Foo.__all__ = ['Bar']
help(Foo)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.