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 trialRegina Ackah
2,649 PointsUnderstanding Class, object and Instance
So in the video.
she passed this code
below is the class created.
# class Car():
# pass
below is supposedly creating an object with class. the created object is my_car. this process is called instatiation.
#my_car = Car()
Does this mean, the variable "my_car" is the instance? as well as an object? is the variable an object also?
I am confused.
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsHey Regina Ackah, good question.
A class defined using
class Car:
pass
may be referred to as Car
. When parens are added, as in, Car()
, this causes the creation of an instance of the class. This object is usually assigned to a label, such as my_car
that points to the new created instance.
So "variables" are basically labels that point to objects. This is how objects are passed into functions. The label is passed to a function that, in turn, points it's local parameter label to the same object.
So my_car
is the label (aka variable) that points to an instance of Car
.
Going a bit further, once the class Car
has been defined, it is the label Car
that is pointed to the class.
Whoa, what's with all these labels? Also a good question. These label references to objects are all organized into namespaces, so that the scope of where they have meaning is well defined.
Post back if you have more questions. Good luck!
Regina Ackah
2,649 PointsRegina Ackah
2,649 PointsSo I had to sleep on it and rewatch the videos to finally understand.
I get it perfectly now. your explanation helped a lot!!!!