参数在发送到函数之前发生更改,python 2.7

参数在发送到函数之前发生更改,python 2.7,python,python-2.7,Python,Python 2.7,在您看到代码后,我将更容易解释问题: 头等舱: class Circuit: """creation of a circuit of nodes""" def __init__ (self): self.nodes=dict() self.inputs=dict() self.outputs=dict() def add_node_to_circuit(self,x): if not isinstance(x, Node): raise Typ

在您看到代码后,我将更容易解释问题:

头等舱:

class Circuit:
"""creation of a circuit of nodes"""
def __init__ (self):
    self.nodes=dict()
    self.inputs=dict()
    self.outputs=dict()

def add_node_to_circuit(self,x):
     if not isinstance(x, Node):
        raise TypeError('Node expected')

     if not self.nodes.has_key(x):
        self.nodes[x]=None
     else : print "Node already exist .error in add node to circuit because user not allowed changing nodes in circuit after they created"
二等舱:

class Node:
def __init__ (self):
    """equals to default constuctor in c + setting the variables
    do not read this constructor directly"""
    self.name=[]
    self.input_one=[]
    self.input_two=[]
    self.output=[]
    self.action=[]
    self.size=0
    ##print "constructor has been read"   self checking
    ##print "constructor self= " ,self
    ##print " with :",name,input_one,input_two,output,action

def read_from_line_anygate(self,line):
    if isinstance(line,list)==False : print "error with line not being a list,anygate"
    self.name=line[0]
    self.input_one=line[1]
    self.input_two=line[2]
    self.output=line[3]
    self.action=line[4]
    self.size=5
def insert_Node(self,line):
    """line needs to be a list type"""
    if len(line)==5 : self.read_from_line_anygate(line)
    elif len(line)==4 : self.read_from_line_gatenot(line)
    else : print "error in insert_Node"
主要内容:

正如您可能看到的,我还有一些其他的方法,但它们非常直观,除了类节点的strrepr之外。 我的问题主要是,除了第一次迭代外,每当x立即更改时,w.nodes都会在w.add_node_to_电路的行被读取之前更改它,并使用调试器进行检查,而且它会删除字典节点中的最后一个键,而不是将其添加到存在的节点中。 我还尝试在主w节点中打印,w输入,w输出这是我得到的:

 Node already exist .error in add node to circuit because user not allowed changing nodes in circuit after they created
Node already exist .error in add node to circuit because user not allowed changing nodes in circuit after they created
Node already exist .error in add node to circuit because user not allowed changing nodes in circuit after they created
Node already exist .error in add node to circuit because user not allowed changing nodes in circuit after they created
Node already exist .error in add node to circuit because user not allowed changing nodes in circuit after they created
Node already exist .error in add node to circuit because user not allowed changing nodes in circuit after they created
Node already exist .error in add node to circuit because user not allowed     changing nodes in circuit after they created
Node already exist .error in add node to circuit because user not allowed             changing nodes in circuit after they created
{['X_c2_d3', '_c2_net3', 'inp3', '_net3', 'NAND']: None}
{'inp1': None, 'inp3': None, 'inp2': None}
{'outp': None, '_outp2': None, '_outp3': None}    

我使用的是python 2.7。

您不是在main中创建新节点,而是每次修改
x
。因为它是作为引用传递的,所以它总是相同的。为了避免这种情况,您需要在循环内创建
x

w=Circuit()

g = open("cir1.gatelevel","r")
    for l in g:
        x=Node()
        x.insert_Node(l.strip().split())
        w.add_node_to_circuit(x)

为什么
Node
insert\u Node
方法?为什么你一直试图将
x
节点添加到
w
?至于问题的第一部分,我试图构建一个类似于图形、树或森林的节点(最后我希望它解决一个逻辑门电路)。我同意名称insert_Node不是最好的,但我还有什么其他选择?在init中执行它?对于第二部分,我想在最后得到3个字典,一个用于输入,一个用于输出,还有一个将包含我电路中的所有节点。我还没有完全弄清楚,但下一步将创建另一个类,wire,它将节点从一个连接到另一个。你知道了,它正在工作,谢谢。我还有一个后续问题,在C/C++中,我可以立即看到参数是通过值、指针还是引用发送的。我如何在python中看到它们之间的区别呢?@maor在python中所有内容都是通过引用传递的。如果它是一个不可变的对象,你将永远不会看到任何副作用,但是如果它是可变的,你需要小心,不要修改它或者复制它。@ MAOR:如果你习惯C++变量语义,你可能会发现下面的文章有用:我也会推荐NETWorkX用于任何与图论相关的东西。我不太清楚你想说什么,但networkx是一个非常通用的图论模块
w=Circuit()

g = open("cir1.gatelevel","r")
    for l in g:
        x=Node()
        x.insert_Node(l.strip().split())
        w.add_node_to_circuit(x)