Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialtyler borg
9,429 PointsI am so lost with this video, I've watched it like 5 times but I'm still not understanding Methods.
I've rewatched this video like 5 times but I'm still not understanding Methods. Kenneth just seems to speed through all this complex stuff assuming we know it without actually explaining how it works. I feel very stuck.
2 Answers
Steven Parker
231,275 PointsMethods are just functions, that also happen to be attributes of a class. You get to them with a dot (.) just like you get to the kind of attributes that store values. That's basically it.
Did you have any specific questions about them?
tyler borg
9,429 PointsI'm just having a hard time remembering what's what because it all looks so similar. So the Class always comes before the dot and the attribute always comes after? But an instance can also come before?
Kailash Seshadri
3,087 PointsYep. From what I understand, an attribute is a variable inside a Class. A method is a function inside a class.
If you you say Class.attribute
, the result of that is the attribute of the Class. When you say Class.method()
, the result of the code is the output of the function which is the method
Say we have a class called Car here:
class Car:
brand = "Toyota"
Num_Wheels = 4
def noise(self):
print("vroom")
The Num_Wheels and brand are attributes, and the noise() is a method.
class Car:
brand = "Toyota"
Num_Wheels = 4
def noise(self):
print("vroom")
car_instance = Car()
When we say car_instance = Car()
, car_instance is an instance. Think of an instance as a copy of the class.
We can take find the attributes of this instance using the .
car_instance.Num_Wheels
is 4 and car_instance.brand
is "Toyota". These strings and numbers can be stored into variables or printed. Saying print(car_instance.brand)
will output "Toyota".
We can also run the functions inside the class, i.e. methods. Saying car_instance.sound()
will print "vroom"
class Car:
brand = "Toyota"
Num_Wheels = 4
def noise(self):
print("vroom")
car_instance = Car()
Car.brand = "Mercedes"
car_instance.brand = "Toyota"
What we can also do is change the attributes of a class. When we say Car.brand = "Mercedes"
, the brand attribute, which used to be "Toyota" had now changed to "Mercedes". This affects the original class, and hence also affects the car_instance
copy. car-instance.brand
is also "Mercedes"
Say we dont want that. What we want is the instance's brand to be "Toyota", but the original class's brand to remain "Mercedes" . We can say car_instance.brand = "Toyota"
. This will change the brand of car_instance
, without affecting the original Car class.
So you end up with Car.brand
as "Mercedes", and car_instance.brand
as "Toyota"
Hope this helps!
tyler borg
9,429 Pointstyler borg
9,429 PointsI'm just having a hard time remembering what's what because it all looks so similar. So the Class always comes before the dot and the attribute always comes after? But an instance can also come before?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsIt's much more common to have "
instance.attribute
" (or "instance.method()
"). You might see "Class.method()
", but only if it is a specially-defined class method.