i am currently working on a producion optimization tool. There are Jobs who needs to be produced on machines.
I want to add a workspeed for the machines so they can finish the jobs in a different speed depending on the machine it is processed on.
This is the class and list i am using for the machines:
JavaScript
x
11
11
1
number_of_machines = 3
2
3
class Machine(object):
4
def __init__(self, workload, workspeed):
5
self.workload = workload
6
self.workspeed = workspeed
7
8
all_machines =[]
9
for i in range(number_of_machines):
10
all_machines.append(Machine(0)) #puts the workload of all machines to 0 before the loop starts
11
i want to add a different float(for example 0.5 , 1.0 , 1.5) to each object in the list “all_machines”
Help would be very much appreciated!
Thanks in advance!
Advertisement
Answer
If you want workload
to always have an initial value of 0 just define it in the class and remove the parameter from the __init__
JavaScript
1
7
1
class Machine(object):
2
3
workload = 0
4
5
def __init__(self, workspeed):
6
self.workspeed = workspeed
7
If you want to keep __init__
with those two parameters you can use default value (notice the order of the parameters is reversed)
JavaScript
1
6
1
class Machine(object):
2
3
def __init__(self, workspeed, workload=0):
4
self.workload = workload
5
self.workspeed = workspeed
6
For both options
JavaScript
1
10
10
1
all_machines = [Machine(0.5), Machine(1.0), Machine(1.5)]
2
3
for machine in all_machines:
4
print(f'{machine.workload} {machine.workspeed}')
5
6
# output:
7
# 0, 0.5
8
# 0, 1.0
9
# 0, 1.5
10