【发布时间】:2016-12-12 00:24:37
【问题描述】:
我正在尝试使用函数注释,希望我的编辑器能够更好地重构。然而,我绊倒了以下问题:
我有一个抽象基类算法。
class Algorithm(metaclass=ABCMeta):
def __init__(self):
self.foo = 'bar'
我还有一个使用算法子类实例的函数
def get_foo(foo_algorithm):
return foo_algoritm.foo
输入 foo_algorithm 可以是任何算法子类的实例。我如何明智地注释这个输入?我正在寻找类似的东西:
def get_foo(foo_algorithm: subclassof(Algorithm)):
return foo_algoritm.foo
但我找不到正确的方法。
【问题讨论】:
-
你的问题有点不清楚。你说的好像你的
get_foo会接受Algorithm的子类,而不是Algorithm子类的instance。您的代码另有说明;.foo在__init__方法中设置,因此必须是实例属性。它不会出现在类对象上。 -
好点,已编辑。它确实是我正在使用的实例化子类。
-
好吧,无论哪种方式,我现在都涵盖了。
-
谢谢你解决了我的问题,我猜python总是比我想象的简单:)。
标签: python python-3.x annotations type-hinting