简体   繁体   中英

I cannot understand a case of passing an object as a parameter

I have a class Node with a function defined

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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