Classes - self.argument=argument


#1

May I know whether when we declare a variable argument=self.argument in a class method, isn’t argument the same as self.argument then?

E.g.

class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg

def display_car(self):
print “This is a %s %s with %s MPG.” % (self.color, self.model, str(self.mpg))

Instead of typing self.color, self.model, … , I tried to just type color, model, str(mpg) bcs I thought they were equivalent, but it doesn’t work, at least in the Codecademy environment. May I know why is that so? Thank you!


#2

argument is the passed in value when invoking a new instance, self.argument is the value written to the closure of the instance. The argument itself is forgotten once the instance is defined.


#3

That’s really helpful. Thank you so much!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.