I have the following Python object:
class car(object):
def __init__(self, name, cost, type, speed):
self.name = name
self.cost = cost
self.type = type
self.speed = speed
def get_name(self): return self.name
def get_cost(self): return self.cost
def get_type(self): return self.type
def get_speed(self): return self.speed
I then want to perform the same operation to some parameters of the object. I want to add 1 to each specified attribute.
One way of doing this is obviously:
obj = car('volvo', 100, 0, 5)
obj.cost = obj.cost + 1
obj.type = obj.type + 1
obj.speed = obj.speed + 1
But this wastes several lines of code. Is there a way to do something like:
attributesToModify = ['cost', 'type', 'speed']
for e in attributesToModify:
obj.e = obj.e + 1