3

I'm fairly green to OOP and I was just playing around with it in Python and came across something I can't explain so hopefully you guys will be able to help.

I was playing with the code below:

class Car():
 def __init__(self, brand, model, speed):
 self.brand = brand
 self.model = model
 self.speed = speed
 def increase_speed(self):
 return self.speed + 1
 def decrease_speed(self, decrease_by):
 return self.speed - decrease_by
car1 = Car("tesla","x",30)
print(car1.brand)
print(car1.speed)
print(car1.increase_speed())
print(car1.speed)
print(car1.decrease_speed(10))

My question is, I am expecting after increasing the speed, car1's speed will be 31 but instead it prints out 30. Why is it that way and how should the code be written for the speed to be 31 instead?

hiro protagonist
47.4k17 gold badges93 silver badges119 bronze badges
asked Mar 15, 2019 at 12:22
3
  • 3
    You haven't assigned the increased speed back to the origial self.speed. Thus increase_speed didn't really increased the speed; it just showed 1 unit higher speed and gone. Commented Mar 15, 2019 at 12:24
  • ....and BTW that's not a problem of OOP, but just the "P" in OOP Commented Mar 15, 2019 at 12:26
  • ...a = 5 now print( a + 7 ). What is the value of a? Commented Mar 15, 2019 at 12:29

3 Answers 3

3
def increase_speed(self):
 self.speed += 1
 return self.speed

Previously you did not increase your speed, rather you just return a value that is equal to the speed plus 1. Similarly, change your decrease_speed function.

answered Mar 15, 2019 at 12:25
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of returning the values, set them on the attributes:

class Car():
 def __init__(self, brand, model, speed):
 self.brand = brand
 self.model = model
 self.speed = speed
 def increase_speed(self):
 self.speed = self.speed + 1
 def decrease_speed(self, decrease_by):
 self.speed = self.speed - decrease_by

I deliberately don't return the changed speed anymore, as it's good style (at least with methods mainly setting attributes) to either return something or change state:

car1 = Car("tesla","x",30)
print(car1.brand)
print(car1.speed)
car1.increase_speed()
print(car1.speed)
car1.decrease_speed(10)
print(car1.speed)
answered Mar 15, 2019 at 12:25

Comments

1

The method increase_speed is just returning self.speed + 1, if you wish to update the speed you need to self.speed = self.speed + 1 into the method increase speed.

Samsul Islam
2,6192 gold badges20 silver badges24 bronze badges
answered Mar 15, 2019 at 12:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.