删除现有的类变量产生 AttributeError

Chi*_*kus 7 python metaclass

我正在通过 Python 的元类操纵类的创建。但是,尽管类由于其父类而具有属性,但我无法删除它。

class Meta(type):
    def __init__(cls, name, bases, dct):
        super().__init__(name, bases, dct)
        if hasattr(cls, "x"):
            print(cls.__name__, "has x, deleting")
            delattr(cls, "x")
        else:
            print(cls.__name__, "has no x, creating")
            cls.x = 13
class A(metaclass=Meta):
    pass
class B(A):
    pass
Run Code Online (Sandbox Code Playgroud)

上面代码的执行产生一个AttributeErrorwhen 类B被创建:

A has no x, creating
B has x, deleting
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-49e93612dcb8> in <module>()
     10 class A(metaclass=Meta):
     11     pass
---> 12 class B(A):
     13     pass
     14 class C(B):

<ipython-input-3-49e93612dcb8> in __init__(cls, name, bases, dct)
      4         if hasattr(cls, "x"):
      5             print(cls.__name__, "has x, deleting")
----> 6             delattr(cls, "x")
      7         else:
      8             print(cls.__name__, "has no x, creating")

AttributeError: x
Run Code Online (Sandbox Code Playgroud)

为什么我不能删除现有的属性?

编辑:我认为我的问题与类实例上的delattr不同,它会产生意外的 AttributeError,它试图通过实例删除类变量。相反,我尝试通过类(别名实例)删除类变量(别名实例)。因此,给定的修复在这种情况下不起作用。

EDIT2: olinox14 是对的,这是“删除父类的属性”的问题。问题可以简化为:

class A:
    x = 13
class B(A):
    pass
del B.x
Run Code Online (Sandbox Code Playgroud)

oli*_*x14 5

看起来 python 将x变量注册为 A 类的参数:

捕获

然后,当您尝试从B类中删除它时,该方法会发生一些冲突delattr,就像@David Herring 提供的链接中提到的那样......

解决方法可以是从类中显式删除参数A

delattr(A, "x")
Run Code Online (Sandbox Code Playgroud)


jsb*_*eno 2

正如您在简化版本中得出的结论,发生的事情很简单:属性“x”不在类中,它在超类中,正常的 Python 属性查找将从那里获取它以供读取 - 以及在写入时,即,设置一个新的cls.x将在他的子类中创建一个本地x:

In [310]: class B(A): 
     ...:     pass 
     ...:                                                                                                                         

In [311]: B.x                                                                                                                     
Out[311]: 1

In [312]: del B.x                                                                                                                 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-312-13d95ac593bf> in <module>
----> 1 del B.x

AttributeError: x

In [313]: B.x = 2                                                                                                                 

In [314]: B.__dict__["x"]                                                                                                         
Out[314]: 2

In [315]: B.x                                                                                                                     
Out[315]: 2

In [316]: del B.x                                                                                                                 

In [317]: B.x                                                                                                                     
Out[317]: 1
Run Code Online (Sandbox Code Playgroud)

不过,如果您需要抑制继承类中的属性,则可以通过元类中的自定义__getattribute__方法(不是)。__getattr__甚至不需要元类上的其他方法(尽管您可以使用它们,例如编辑要抑制的属性列表)

class MBase(type):
    _suppress = set()

    def __getattribute__(cls, attr_name):
        val = super().__getattribute__(attr_name)
        # Avoid some patologic re-entrancies
        if attr_name.startswith("_"):
            return val
        if attr_name in cls._suppress:
            raise AttributeError()

        return val


class A(metaclass=MBase):
    x = 1

class B(A):
    _suppress = {"x",}
Run Code Online (Sandbox Code Playgroud)

如果有人试图得到B.x它,它就会升高。

通过这一策略,向元类添加__delattr__方法__setattr__ 可以删除在子类上的超类中定义的属性:

class MBase(type):
    _suppress = set()

    def __getattribute__(cls, attr_name):
        val = super().__getattribute__(attr_name)
        # Avoid some patologic re-entrancies
        if attr_name.startswith("_"):
            return val
        if attr_name in cls._suppress:
            raise AttributeError()

        return val

    def __delattr__(cls, attr_name):
        # copy metaclass _suppress list to class:
        cls._suppress = set(cls._suppress)
        cls._suppress.add(attr_name)
        try:
            super().__delattr__(attr_name)
        except AttributeError:
            pass

    def __setattr__(cls, attr_name, value):
        super().__setattr__(attr_name, value)
        if not attr_name.startswith("_"):
            cls._suppress -= {attr_name,}



class A(metaclass=MBase):
    x = 1

class B(A):
    pass
Run Code Online (Sandbox Code Playgroud)

在控制台上:

In [400]: B.x                                                                                                                     
Out[400]: 1

In [401]: del B.x                                                                                                                 

In [402]: B.x                                                                                                                     
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
... 

In [403]: A.x                                                                                                                     
Out[403]: 1
Run Code Online (Sandbox Code Playgroud)