首页 > 解决方案 > 使用 self.__setattr__() 时如何不搞乱 self.__dict__ 生成

问题描述

我正在尝试实现特殊行为,以便 self 和 self.aux_gate 中的值始终相同。我尝试通过实现__setattr__()来做到这一点,但在这样做时,我遇到了很多关于__dict__.

据我所知,似乎每当self.__setattr__()正常调用时,它都会向 中添加一个键值对self.__dict__,对应于self.__setattr__()给定的参数。

但是,self.__setattr__()我写的似乎创建了额外的self.__dict__条目,我已经看到了self.__dict__["conditional"]self.__dict__["current_Position"]self.__dict__["current_Track"]。我看不出这些价值观是如何进入self.__dict__.

这是课程,我在下面复制并粘贴了它的超类

class SWAP_Gate(Quantum_Gate):
    
    def __init__(self, cost, conditional, current_Track, current_Position, rectangle = None, aux_gate = None):
        if aux_gate is None:
            self.aux_gate = SWAP_Gate(cost,conditional,current_Track,current_Position, aux_gate = self)
        else:
            self.aux_gate = aux_gate
        self.cost = cost
        self.__SWAPconditional = conditional
        self.__SWAPcurrent_Track = current_Track
        self.__SWAPcurrent_Position = current_Position
        if rectangle is None:
            self.rectangle = pg.Rect(430, 0, 1480, 150)
        else:
            self.rectangle = rectangle
    
    def __setattr__(self, name, value):
        super(SWAP_Gate, self).__setattr__(name, value)
        if name == "conditional":
            self.aux_gate.conditional = value
        #I've deleted some code on this line that jsut made the whole post a lot harder to read, it's not relevant to my problem
        elif name == "current_Position":
            self.current_Position = value
            self.aux_gate.current_Position = value
        elif name == "aux_gate":
            self.__dict__["aux_gate"] == value
        else:
            self.__dict__[name] == value
    
    def __getattr__(self, name):
        if name == "conditional":
            return self.__SWAPconditional
        elif name == "current_Track":
            return self.__SWAPcurrent_Track
        elif name == "current_Position":
            return self.__SWAPcurrent_Position
class Quantum_Gate():
        
    def __init__(self, cost, conditional, current_Track, current_Position, rectangle = None):
        self.cost = cost
        self.conditional = conditional
        self.current_Track = current_Track
        self.current_Position = current_Position
        if rectangle is None:
            self.rectangle = pg.Rect(0, 0, 100, 100)
        else:
            self.rectangle = rectangle```

标签: pythondictionarysetattr

解决方案


我看到的主要是你__setattr__函数中的第一条语句是

super(SWAP_Gate, self).__setattr__(name, value)

除了您在 中的任何其他代码之外,此语句将导致默认的 setattr 行为发生,因此在任何时候都可以__setattr__看到“current_Track” (除非 SWAP_Gate 的超类阻止了 setattr 的默认行为)。__dict__current_Track

如果您不希望发生该默认行为,则删除顶部的 super() 语句并在最后的 else 语句中用它__setattr__替换该语句:self.__dict__

def __setattr__(self, name, value):
        # Removed super() statement from here
        if name == "conditional":
            self.aux_gate.conditional = value
        elif name == "current_Track":
            if self.current_Track:
        #...
        elif name == "aux_gate":
            self.__dict__["aux_gate"] == value
        else:
            #replaced self.__dict__[name]=value with super() statement:
            super(SWAP_Gate, self).__setattr__(name, value)

推荐阅读