python - 使用 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```
解决方案
我看到的主要是你__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)
推荐阅读
- java - jsp mvc项目上的java空指针异常
- apache-kafka - 在 JHipster 微服务环境中使用 Eureka 和 Zookeper(Kafka) 是否有任何开销?
- javascript - d3 sankey 图表未呈现
- caffe - Caffe - CNN 错误:RuntimeWarning:在除法中遇到除以零
- c# - 使用 C# 的交互式输入?
- javascript - FirebaseUI 登录完成后,Firebase 令牌如何传递到重定向 url?
- python - 如何在python中导入opencv?
- node.js - aws FIFO 队列返回空队列,即使它有可用的消息
- java - ListView 上的 JavaFX RemoteException?
- javascript - 使用javascript计算单词 - 邮递员测试