簡體   English   中英

我無法理解將對象作為參數傳遞的情況

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM