英文:
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 the correct sense or not?
I tried giving the 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.
英文:
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.
答案1
得分: 0
这是因为在transfer_task_passed_minimum_from_unassigned_production_tasks
函数的作用域中没有名为time
的对象。你应该首先将一个对象绑定到这个名称,然后才能使用它。
你可以在completed_transfer_tasks
函数中将time
设置为一个对象变量,然后尝试通过对象本身在transfer_task_passed_minimum_from_unassigned_production_tasks
方法中访问它。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论