Python抽象装饰不起作用

标签 python abstract-class

我正在编写一个装饰(出于好奇)以在 Python 中创建一个抽象类。到目前为止,它看起来可以正常工作,但我遇到了意外行为。

装饰的想法是这样的:

from abc import ABCMeta, abstractmethod

def abstract(cls):
    cls.__metaclass__ = ABCMeta
    return cls

那么在使用这个装饰的时候,只需要定义一个抽象方法就可以了

@abstract
class Dog(object):
   @abstractmethod
   def bark(self):
      pass

但是当我测试时,我能够实例化一个 Dog 对象:

d = Dog()
d.bark() //no errors
Dog.__metaclass__ //returned "<class 'abc.ABCMeta'>"

当测试直接分配 __metaclass__ 时,它的行为符合预期:

class Dog(object):
   __metaclass__ = ABCMeta
   @abstractmethod
   def bark(self):
      pass

测试:

d = Dog()

"Traceback (most recent call last):
  File "<pyshell#98>", line 1, in <module>
    d = Dog()
TypeError: Can't instantiate abstract class Dog with abstract methods bark"

为什么会这样?

最佳答案

python 引用 states :

When the class definition is read, if __metaclass__ is defined then the callable assigned to it will be called instead of type(). This allows classes or functions to be written which monitor or alter the class creation process

重要的部分是当类定义被读取时,这意味着您之后不能更改元类,而这正是装饰器试图做的,因为它只是语法糖。

@some_decorator
class SomeClass(object):
    pass

等同于:

class SomeClass(object):
    pass

SomeClass = some_decorator(SomeClass)

所以当装饰器被调用时,类定义已经被读取了。

关于Python抽象装饰不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32640184/

相关文章:

python - 如何让selenium等待多个元素加载

python - 重复函数提取相似信息

python - Python 中按下按键时暂停和继续循环

Python minidom 获取子节点值时遇到问题

java - 在抽象类中调用重写的函数

.net - 导航到通用定义实体的正确方法

Python循环运行一定的秒数

c# - 用具体类型覆盖抽象方法

java - 在它的抽象父类(super class)中使用子类的泛型类型?

python - 强制子类编写文档字符串