Search code examples
pythonfunctionoopmethodsnameerror

calling method inside a method which takes some argument


I am doing object oriented programming and quite new to it. I have defined classes and methods but I am facing some problem when I try to call one method which takes some argument into another method.It gives me: NameError: name 'time' is not defined.

Below is the snippet of my code.

@dataclass
class Stage:
    id: int
    production_equipment: List[ProductionEquipment] | None = None
    transfer_equipment: List[TransferEquipment] | None = None

    def completed_transfer_tasks(self, time: int):

        if self.transfer_equipment is not None:

            completed_transfer_task_ids = []

            for equipment in self.transfer_equipment:

                if equipment.transfer_tasks is not None:

                    for task in equipment.transfer_tasks:

                        if time - task.start_time >= task.min_transfer_duration:
                            completed_transfer_task_ids.append(task.id)

            return completed_transfer_task_ids

        else:

            print('Stage has TransferEquipment None')

I am trying to use this method in another one like below:

 def transfer_task_passed_minimum_from_unassigned_production_tasks(self):

        production_task_ids = self.unassigned_production_tasks_in_stage2and3()
        transfer_task_ids = self.completed_transfer_tasks(time)

        for id in production_task_ids:
            if id in transfer_task_ids:
                return id

        return None

When I try to call this method like below,

stage2 = Stage(2, [equipment1], [equipment2, equipment3])

if __name__ == '__main__':
    stage2.completed_transfer_tasks(70)
    x = stage2.transfer_task_passed_minimum_from_unassigned_production_tasks()
    print(x)

I am not sure if I am referencing the method in correct sense or not?

I tried giving time parameter initially, but this is not to be expected as it should be defined as an argument.

I think I am missing some syntax of calling methods which I am unable to rectify.


Solution

  • This is because there is no object named time in the scope of transfer_task_passed_minimum_from_unassigned_production_tasks function. You should first bind an object to this name then you can use it.

    You can set time as an object variable in the completed_transfer_tasks function, and then try to access it through the object itself in the transfer_task_passed_minimum_from_unassigned_production_tasks method.

    
    @dataclass
    class Stage:
        id: int
        production_equipment: List[ProductionEquipment] | None = None
        transfer_equipment: List[TransferEquipment] | None = None
    
        def completed_transfer_tasks(self, time: int):
            self._time = time        
            if self.transfer_equipment is not None:
    
                completed_transfer_task_ids = []
    
                for equipment in self.transfer_equipment:
    
                    if equipment.transfer_tasks is not None:
    
                        for task in equipment.transfer_tasks:
    
                            if time - task.start_time >= task.min_transfer_duration:
                                completed_transfer_task_ids.append(task.id)
    
                return completed_transfer_task_ids
    
            else:
    
                print('Stage has TransferEquipment None')
    
     def transfer_task_passed_minimum_from_unassigned_production_tasks(self):
    
            production_task_ids = self.unassigned_production_tasks_in_stage2and3()
            transfer_task_ids = self.completed_transfer_tasks(self._time)
    
            for id in production_task_ids:
                if id in transfer_task_ids:
                    return id
    
            return None