检查 self.__class__ 的目的是什么? [英] What is the purpose of checking self.__class__?
问题描述
检查 self.__class__
的目的是什么?我发现了一些创建抽象接口类的代码,然后检查它的 self.__class__
是否是它本身,例如
class abstract1(对象):def __init__(self):如果 self.__class__ == abstract1:raise NotImplementedError("接口不能被实例化")
这样做的目的是什么?是检查类是否是自身的类型?
代码来自 NLTK 的 http:///nltk.googlecode.com/svn/trunk/doc/api/nltk.probability-pysrc.html#ProbDistI
self.__class__
是对当前实例的类型的引用.
对于 abstract1
的实例,那就是 abstract1
类本身,这是抽象类不想要的.抽象类仅用于子类化,而不是直接创建实例:
对于abstract1
的子类 的实例,self.__class__
将是对特定子类的引用:
在此处抛出异常就像在代码的其他地方使用 assert
语句一样,它可以防止您犯下愚蠢的错误.
请注意,pythonic 测试实例类型的方法是使用 type()
函数,以及带有 is
运算符的 identity 测试:
class abstract1(object):def __init__(self):如果 type(self) 是 abstract1:raise NotImplementedError("接口不能被实例化")
type()
应该优先于 self.__class__
因为后者 可以被类属性遮蔽.
这里对于自定义类使用相等测试没有什么意义,__eq__
基本上是作为身份测试实现的.
Python 还包含一个标准库来定义抽象基类,称为 abc代码>
.它允许您将方法和属性标记为抽象,并拒绝创建尚未重新定义这些名称的任何子类的实例.
What is the purpose of checking self.__class__
? I've found some code that creates an abstract interface class and then checks whether its self.__class__
is itself, e.g.
class abstract1 (object):
def __init__(self):
if self.__class__ == abstract1:
raise NotImplementedError("Interfaces can't be instantiated")
What is the purpose of that? Is it to check whether the class is a type of itself?
The code is from NLTK's http://nltk.googlecode.com/svn/trunk/doc/api/nltk.probability-pysrc.html#ProbDistI
self.__class__
is a reference to the type of the current instance.
For instances of abstract1
, that'd be the abstract1
class itself, which is what you don't want with an abstract class. Abstract classes are only meant to be subclassed, not to create instances directly:
>>> abstract1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __init__
NotImplementedError: Interfaces can't be instantiated
For an instance of a subclass of abstract1
, self.__class__
would be a reference to the specific subclass:
>>> class Foo(abstract1): pass
...
>>> f = Foo()
>>> f.__class__
<class '__main__.Foo'>
>>> f.__class__ is Foo
True
Throwing an exception here is like using an assert
statement elsewhere in your code, it protects you from making silly mistakes.
Note that the pythonic way to test for the type of an instance is to use the type()
function instead, together with an identity test with the is
operator:
class abstract1(object):
def __init__(self):
if type(self) is abstract1:
raise NotImplementedError("Interfaces can't be instantiated")
type()
should be preferred over self.__class__
because the latter can be shadowed by a class attribute.
There is little point in using an equality test here as for custom classes, __eq__
is basically implemented as an identity test anyway.
Python also includes a standard library to define abstract base classes, called abc
. It lets you mark methods and properties as abstract and will refuse to create instances of any subclass that has not yet re-defined those names.
这篇关于检查 self.__class__ 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!