我可以在python中做到这一点,它给了我函数内可用的子模块/参数。探测python函数
在解释,我可以这样做:
>>> from nltk import pos_tag
>>> dir(pos_tag)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
顺便说一句,什么是dir(function)
电话吗?
如何知道调用函数需要哪些参数?例如在pos_tag
的情况下,源代码的说,它需要token
,看到https://github.com/nltk/nltk/blob/develop/nltk/tag/init.py
def pos_tag(tokens):
"""
Use NLTK's currently recommended part of speech tagger to
tag the given list of tokens.
>>> from nltk.tag import pos_tag # doctest: +SKIP
>>> from nltk.tokenize import word_tokenize # doctest: +SKIP
>>> pos_tag(word_tokenize("John's big idea isn't all that bad.")) # doctest: +SKIP
[('John', 'NNP'), ("'s", 'POS'), ('big', 'JJ'), ('idea', 'NN'), ('is',
'VBZ'), ("n't", 'RB'), ('all', 'DT'), ('that', 'DT'), ('bad', 'JJ'),
('.', '.')]
:param tokens: Sequence of tokens to be tagged
:type tokens: list(str)
:return: The tagged tokens
:rtype: list(tuple(str, str))
"""
tagger = load(_POS_TAGGER)
return tagger.tag(tokens)
如果文档字符串可用于功能是有没有办法知道什么是参数类型的函数需要一个具体参数?,例如在pos_tag
以上的案例中,它的编号为:param tokens: Sequence of tokens to be tagged
和:type tokens: list(str)
运行解释器时未读取代码就可以获取这些信息吗?
最后,有没有办法知道什么是返回类型?
只是要清楚,我不期待的文档字符串的打印输出,但上面的问题是,这样我可以做某种类型与isinstance(output_object, type)
很好的答案!!!! – alvas 2014-12-03 13:27:43
@alvas你问很好的问题!我爱你如何深入挖掘nltk。 – 2014-12-03 13:51:44