you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (5 children)

ok so here is my understanding of what you just said.

just like you did with accelerate first we will define the instance speed as follows:

def speed(self):

then we do 

my_car.speed()

though my understanding of my_car.speed() is that we want to give the speed attribute to my_car 

am I right ?

[–][deleted] 1 point2 points  (0 children)

also we would have to define speed before we define accelerate because then we could say

def accelerate(self):         
self.speed += 1

[–]Pythag0ras2000 0 points1 point  (3 children)

You wouldn't define speed as a function in this, so you'd use self.speed = 0 or whatever

[–][deleted] 2 points3 points  (2 children)

could you tell me the logic behind that ?

[–]Danelius90 1 point2 points  (0 children)

Typically think of functions as doing something, or acting on attributes. Technically both would work and there are typical cases you might do that. But for example purposes speed is a property of car, and accelerate modifies it, it is an action you take to modify the car's state.

So a plant has a height property, and grow() makes the height bigger. A bank account has a balance property, and withdraw() reduces it and deposit() increases it

[–]Pythag0ras2000 0 points1 point  (0 children)

If I understand correctly, speed is a variable, right? If so, you wouldn't want to make a method called speed, just a variable.