是否有一种内置的方法可以用于获取Python中的规范注释,而不是返回并在类定义中读取它们?具体来说,我试图确定导入模块中的对象有多少个参数以及哪种参数。是否有显示对象规格的Python函数或方法?
我在Python文档中找不到这个答案,如果它已经在这里得到解答,我无法在搜索中找到答案。
是否有一种内置的方法可以用于获取Python中的规范注释,而不是返回并在类定义中读取它们?具体来说,我试图确定导入模块中的对象有多少个参数以及哪种参数。是否有显示对象规格的Python函数或方法?
我在Python文档中找不到这个答案,如果它已经在这里得到解答,我无法在搜索中找到答案。
>>> from random import randint
>>> help(randint)
Help on method randint in module random:
randint(self, a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.
(END)
http://docs.python.org/2.7/library/functions.html#help
与自定义类:
>>> class Test(object):
... def __init__(self, smile):
... print(smile)
...
>>> help(Test)
Help on class Test in module __main__:
class Test(__builtin__.object)
| Methods defined here:
|
| __init__(self, smile)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
(END)
如果你使用ipython作为shell:'Test?' –
您还可以使用inspect模块,具体inspect.getmembers():
inspect.getmembers(object[, predicate])
返回按名称排序的(名称,值)对列表中的所有 对象的成员。
例子:
>>> import inspect
>>> class Test(object):
... def __init__(self, smile):
... print(smile)
...
>>> inspect.getmembers(Test, predicate=inspect.ismethod)
[('__init__', <unbound method Test.__init__>)]
你可以尝试安装ipython交互shell和使用%pdoc
,%pdef
宏:
%PDOC:打印(或通过寻呼机运行,如果时间过长)一个对象的 文档字符串。如果给定的对象是一个类,它将打印 类和构造函数文档字符串。
%pdef:打印 任何可调用对象的定义标头。如果对象是 类,则打印构造函数信息。
您可以使用Inspect模块。 http://docs.python.org/2/library/inspect.html它可以获得函数名称,参数等 – CoryKramer