gpt4 book ai didi

Python抽象装饰不起作用

转载 作者:太空狗 更新时间:2023-10-29 21:45:56 24 4
gpt4 key购买 nike

我正在编写一个装饰(出于好奇)以在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com