简体   繁体   English

我无法理解将对象作为参数传递的情况

[英]I cannot understand a case of passing an object as a parameter

I have a class Node with a function defined 我有一个带有定义函数的类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

I have another function 我还有另一个功能

def init(node):
    node.setStatus('active')

Now, I have a class 现在,我有一堂课

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

When I create an object of DistAlgo 当我创建DistAlgo的对象时

myalgo = DistAlgo('BMM', init, states, messages, send, receive, stoppingCheck)

and then call its run function: 然后调用其运行功能:

myalgo.run(problemGraph)

I get an error in the init function above, as: 我在上面的init函数中遇到错误,如下所示:

TypeError: setStatus() missing 1 required positional argument: 'status'

I surely am doing more than one thing wrong I guess, as this is my first Python try. 我肯定做错了很多事情,因为这是我第一次尝试Python。 Please point them out! 请指出来!

Properties work a bit differently: 属性的工作方式略有不同:

@property
def status(self):
    return self._status

@status.setter
def status(self, status):
    self._status = status

Now you can set the value with an assignment: 现在,您可以通过分配来设置值:

node.status = 'active'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM