使用 self.__setattr__() 时如何不弄乱 self.__dict__ 代

How to not mess up self.__dict__ generation when using self.__setattr__()

我正在尝试实现特殊行为,以便 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,我把它超级class复制粘贴到下面

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```

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

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

除了您在 __setattr__ 中的任何其他代码之外,此语句将导致默认 setattr 行为发生,因此预计随时会在 __dict__ 中看到“current_Track”current_Track 已设置(除非超级 class 到 SWAP_Gate 阻止了 setattr 的默认行为)。

如果您不希望出现默认行为,请删除 __setattr__ 顶部的 super() 语句,并在最后的 else 语句中用它替换 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)