【发布时间】:2016-09-10 21:04:12
【问题描述】:
所以:
class A(object): pass
class B(A): pass
def class_factory(letter):
"""rtype : type[A]""" # is this correct ?
if letter == 'A': return A # note no parenthesis ! returns the type, not an instance
elif letter == 'B': return B
raise NotImplementedError
似乎找不到任何文档,搜索相当棘手。
奖励:如何在 python 3.5 的类型提示实现中实现这一点?
【问题讨论】:
-
试过
issubclass(B,A)? -
“指定”是什么意思?每个对象都知道它属于哪个类。
-
不是答案,但听起来你可以在
A.__new__()中处理此任务。 -
你为什么要这样做?
标签: python python-2.7 type-hinting