【发布时间】:2014-01-03 03:53:40
【问题描述】:
检查self.__class__ 的目的是什么?我找到了一些创建抽象接口类的代码,然后检查它的self.__class__ 是否是它本身,例如
class abstract1 (object):
def __init__(self):
if self.__class__ == abstract1:
raise NotImplementedError("Interfaces can't be instantiated")
这样做的目的是什么? 是检查类是否是自身的类型吗?
代码来自NLTK的http://nltk.googlecode.com/svn/trunk/doc/api/nltk.probability-pysrc.html#ProbDistI
【问题讨论】:
-
我认为这需要更多的上下文。您在哪里“找到”此代码?它基本上坏了。
-
@Iguananaut:为什么坏了?当按预期使用时,它工作很好;作为一个抽象基类。
-
我很确定之前它是不同的东西?我不知道——我同意,现在那里的东西没有损坏。
标签: python class oop interface self