【发布时间】:2014-12-03 04:22:36
【问题描述】:
我可以在 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) 进行某种类型检查
【问题讨论】:
标签: python function parameters return