假设我有一个看起来像这样的课程:
class FunctionCaller:
def __init__(self):
"""A class which can be used to call different functions which take the same
parameters
"""
self.f = lambda a,b: (a,b)
def setF(self, new_f):
"""Set the function to call
:param new_f: The new function this object should call
:type new_f: func(:class:`.SomeClass`, :class:`int`)
"""
self.f = new_f
def callF(self, a, b):
"""Call the function this object currently contains
:param a: Some value
:param b: Some other value
"""
return self.f(a,b)
class SomeClass:
"""Some class which does nothing
"""
pass
例如(忽略这可能是错误的编码风格的事实),让我们假设FunctionCaller
将要调用的函数期望将SomeClass
作为其第一个参数,并且int作为第二个。我希望文档显示这两个方面的链接。我在示例中定义它的方式有效,但看起来不太好。
有没有办法可以使用:type:
说明符来表明参数是函数?
答案 0 :(得分:0)
有没有办法可以使用
:type:
说明符来表明参数是函数?
我想你可以指定types.FunctionType
作为类型。但我建议您解释一下它的工作原理:
def setF(self, new_f):
"""Set the function to call
:param new_f: The new function this object should call.
The new function takes two positional parameters: the first of
type :class:`.SomeClass` and the second of type :class:`int`.
"""
self.f = new_f