[英]I cannot understand a case of passing an object as a parameter
我有一个带有定义函数的类Node
class Node(object):
def __init__(self, index, state = None, input = None, directed_neighbours=False):
"""
Parameters
----------
index : int
Node index. Must be unique in the graph.
"""
self._input = input
self.state = state
#self._status = 'active'
self._index = int(index)
self._neighbours = set()
self._port_count = 0
self._ports = []
if directed_neighbours:
self._predecessors = set()
self._successors = self._neighbours
self._directed_neighbours = True
else:
self._successors = self._neighbours
self._predecessors = self._neighbours
self._directed_neighbours = False
@property
def setStatus(self, status):
self._status = status
我还有另一个功能
def init(node):
node.setStatus('active')
现在,我有一堂课
class DistAlgo:
def __init__(self, name, initFunc, states, messages, sendFunc, receiveFunc, stoppingCheck):
self.name = name
#self.inputGraph = inputGraph
self.initFunc = initFunc
self.states = states
self.messages = messages
self.sendFunc = sendFunc
self.receiveFunc = receiveFunc
self.comm_round = 0
self.stoppingCheck = stoppingCheck
def run(self, inputGraph):
for node in inputGraph.nodes:
print('hello', node)
node.state = self.initFunc(node)
<....more code...>
当我创建DistAlgo的对象时
myalgo = DistAlgo('BMM', init, states, messages, send, receive, stoppingCheck)
然后调用其运行功能:
myalgo.run(problemGraph)
我在上面的init函数中遇到错误,如下所示:
TypeError: setStatus() missing 1 required positional argument: 'status'
我肯定做错了很多事情,因为这是我第一次尝试Python。 请指出来!
属性的工作方式略有不同:
@property
def status(self):
return self._status
@status.setter
def status(self, status):
self._status = status
现在,您可以通过分配来设置值:
node.status = 'active'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.