TypeError:不支持的操作数类型为+:'int'和'instance'

问题描述:

我对Python并不擅长,我知道某些东西与我的类混为一谈,但我不确定它出了什么问题。这似乎是一个相当普遍的问题,但无论出于什么原因,我都很难理解为什么。TypeError:不支持的操作数类型为+:'int'和'instance'

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) 

它返回错误的标题,当它到达这里:

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线。我只是用它来检查它是否在一段时间后才工作。大脑被炸,出于某种原因,我认为这是足够重要的离开。哎呀。

+0

为什么你的“距离”类存在? – user2357112

+0

什么是错误信息? –

+0

@CaryShindell标题 –

首先,您的距离等级没有__init__方法,这是一个问题。然后,由于您正在尝试添加distance()+1,但您正在尝试添加创建距离类的实例,而不是像您打算的那样实际调用其中的函数。您需要指定一个变量,如d = distance(),并执行d.distance(operator_location, global_location)+1或简单地distance().distance(operator_location, global_location)+1

此外,它似乎并不真正使用距离作为一个类,而是打算做一个全局函数,所以你也可以摆脱class distance行,而不必处理所有的类实例的东西(你只会做distance(operator_location, global_location)+1)。

+1

谢谢,帮助我了解我做错了什么。我只是把它变成了一个功能,没有问题。还取消了+1,这只是一个完整的检查,一会儿我就离开了。 – user3826299

在这两条线:

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

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

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

def distance(operator_location,local_location): 
    ... 
+0

这个工作,我不知道为什么我推翻了它。谢谢。 – user3826299

从我可以告诉确保将所有的数字都是整数

total_walk=total_walk + distance() + 1 

是total_walk和distance()之间的一个整数吗?这也让我感到困惑。我现在可能喋喋不休,但这是我唯一的想法。