TypeError: unsupported operand type(s) for +: 'int' and 'instance'

TypeError: unsupported operand type(s) for +: 'int' and 'instance'

我对 Python 不太满意,我知道我的 class 出了点问题,但我不确定它出了什么问题。这似乎是一个非常普遍的问题,但无论出于何种原因,我都很难理解为什么。

class distance:

    def distance(operator_location,local_location):
        global hsff_conveyor
        global hsff_unload
        global hsdr_conveyor
        global hsdr_unload1
        global hsdr_unload2
        global distance_between_hsff_load_and_hsff_unload
        global distance_between_hsff_load_and_hsdr_load
        global distance_between_hsff_load_and_hsdr_unload1
        global distance_between_hsff_load_and_hsdr_unload2
        global distance_between_hsff_and_hsdr_conveyor
        global distance_between_hsff_unload_and_hsdr_unload1
        global distance_between_hsff_load_and_hsdr_unload2
        global distance_between_hsdr_load_and_hsdr_unload1
        global distance_between_hsdr_load_and_hsdzr_unload2
        global distance_between_hsdr_unload1_and_unload2

        if operator_location==hsff_conveyor and local_location==hsff_unload:
            return distance_between_hsff_load_and_hsff_unload
        elif operator_location==hsff_conveyor and local_location==hsdr_conveyor:
            return distance_between_hsff_load_and_hsdr_load
        elif operator_location==hsff_conveyor and local_location==hsdr_unload1:
            return distance_between_hsff_load_and_hsdr_unload1
        elif operator_location==hsff_conveyor and local_location==hsdr_unload2:
            return distance_between_hsff_load_and_hsdr_unload2
        elif operator_location==hsff_unload and local_location==hsdr_conveyor:
            return distance_between_hsff_and_hsdr_conveyor
        elif operator_location==hsff_unload and local_location==hsdr_unload1:
            return distance_between_hsff_unload_and_hsdr_unload1
        elif operator_location==hsff_unload and local_location==hsdr_unload2:
            return distance_between_hsff_unload_and_hsdr_unload2
        elif operator_location==hsdr_conveyor and local_location==hsdr_unload1:
            return distance_between_hsdr_load_and_hsdr_unload1
        elif operator_location==hsdr_conveyor and local_location==hsdr_unload2:
            return distance_between_hsdr_load_and_hsdr_unload2
        elif operator_location==hsdr_unload1 and local_location==hsdr_unload2:
            return distance_between_hsdr_unload1_and_unload2
        else:
            return int(0)

它returns标题中的错误,当它到达这里时:

def hsff_fill_conveyor(env, operator, logfile):
    global hsff_pick_and_load_time
    global conveyor
    global hsff_conveyor_holds
    global operator_location
    global total_walk
    global total_walk_time
    global hsff_conveyor
    global hsff_unload

    local_location=hsff_conveyor

    while True:

        if operator_location==hsff_conveyor:

            hsff_start_loading_conveyor=env.now
            yield hsff_raw_container_cont.get(hsff_pick_quantity)
            hsff_conveyor_short_time=env.now-hsff_start_loading_conveyor

            with operator.request() as req1:
                yield req1
                hsff_conveyor_waiting_for_operator=env.now-hsff_start_loading_conveyor
                yield env.timeout(hsff_pick_and_load_time)
                hsff_load_cycle_ende=env.now

            yield hsff_conveyor_cont.put(hsff_pick_quantity)

        elif operator_location!=hsff_conveyor:  

            hsff_operator_ready_to_walk=env.now
            hsff_operator_ready_to_walk_short_time=env.now-hsff_operator_ready_to_walk

            with operator.request() as req1:        

                yield req1
                hsff_conveyor_waiting_for_operator=env.now-hsff_operator_ready_to_walk
                yield env.timeout(20) #FILLER
                walk_end=env.now

                total_walk=total_walk + distance() + 1
                operator_location=hsff_conveyor

total_walk = total_walk + distance() + 1 引发错误。我在其他线路中也发生过这种情况。试图模拟一个操作员有五个不同的资源可以请求他。

编辑:+1 不应该在 total_walk 行中。我只是用它来检查它是否能正常工作。大脑被炸了,出于某种原因,我认为离开很重要。哎呀

在这两行中:

class distance:
    def distance(operator_location,local_location):
        ...

您创建了一个 class distance,其中包含一个 方法 ,也称为 distance .这几乎肯定不是您想要的。从你的方法的签名和它的使用我推断你正在尝试创建一个全局函数。

删除两行中的第一行,并将方法的缩进向左移动一步:

def distance(operator_location,local_location):
    ...

据我所知,确保所有相加的数字都是整数

total_walk=total_walk + distance() + 1

total_walk和distance()加1是整数吗?这也让我感到困惑。我现在可能像个白痴一样喋喋不休,但这是我对正在发生的事情的唯一想法。

首先,你的距离 class 没有 __init__ 方法,这是一个问题。然后,您会收到错误消息,因为您尝试添加 distance()+1 但执行 distance() 会创建距离 class 的实例,而不是按照您的意图实际调用其中的函数。您需要分配一个变量,如 d = distance() 并执行 d.distance(operator_location, global_location)+1 或简单地 distance().distance(operator_location, global_location)+1

此外,您似乎并没有真正将距离用作 class,而是打算创建一个全局函数,因此您也可以去掉 class distance 行和不必处理所有 class 实例内容(您只需处理 distance(operator_location, global_location)+1)。