Search code examples
pythonpython-2.7simpy

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


I'm not great with Python, I know something is messed up with my class but I am not sure what is going wrong with it. It seems like a pretty general problem but for whatever reason I am having a difficult time comprehending why.

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)

It returns the error in the title when it gets to here:

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

The total_walk = total_walk + distance() + 1 raises the error. I have this happening in other lines as well. Trying to simulate an operator who has five different resources that can request him.

EDIT: The +1 wasn't suppose to be in the total_walk line. I was just using it to check if it was even working awhile back. Brain is fried and for some reason I thought it was important enough to leave. Opps.


Solution

  • Firstly, your distance class does not have an __init__ method, which is a problem. Then, you are getting your error because you are trying to add distance()+1 but doing distance() creates an instance of the distance class rather than actually calling the function within it as you intend. You need to assign a variable like d = distance() and do d.distance(operator_location, global_location)+1 or simply distance().distance(operator_location, global_location)+1.

    Additionally, it seems that you don't really use distance as a class, but rather intended to make a global function, so you could also just get rid of the class distance line and not have to deal with all of the class instance stuff (you would just do distance(operator_location, global_location)+1).