Differences Between Instance and Object

Hi, I just need clarification on these two terms.

I’ve been told that these two are similar to each other and these terms can be used interchangeably, while the other side presented to me that there are differences between them, so to me, it seems like the more I read about these, the more I get confused because there are different interpretations to the same concepts.

For example:

class Car:

    def __init__(self, color, style):
        self.color = color
        self.style = style

my_car = Car("Black", "Sedan")

So I know that Car is a class, but does it mean that the contents of Car have a group of objects? Like, is this whole thing an object?

    def __init__(self, color, style):
        self.color = color
        self.style = style

And for instance, is self.color and self_style an instance?

It also seems like my_car is an object because as far as I know, we create an object by calling the class and passing in arguments, but then, is the object not already part of the class??

What do I call the contents of the class, because it has properties, attributes and all that stuff. Do I just look at it as a class with a group of objects? And for instance, is my_car not an instance? Can I refer to it as an object or instance or can I use them interchangeably without worrying too much about the way I refer it as?

I’m so confused…

Instance and object are the same thing from what I understand, instance seems like a helper word for us to understand it better.

When you call my_car = Car(“Black”, “Sedan”), you’re instantiating an object. That object’s type is class, and the class type is Car. __init__ is initializing instance variables, these are specific to that object. You can have class variables that apply to all objects that are class Car. I think instance variable is used because object variable isn’t a good description of the scope of the variable.

in·stance

noun

  1. an example or single occurrence of something.

The class itself is best described as a blueprint for creating an object with its own variables and methods. self.color would be an instance variable, in general terms you could say it’s a variable but calling it an instance doesn’t sound right to me. You could say:
my_car is an instance of Car with variables my_car.color and my_car.style
my_car is an object of class Car with variables my_car.color and my_car.style
There is currently one instance of Car with instance variables self.color and self.style
Just in case it’s not confusing enough, here’s an object instantiated in a class, though I’m not sure if it’s appropriate to do such things. :laughing:

class Car: def __init__(self, color, style): self.color = color self.style = style if self.style == "Sedan": self.engine = Engine("3.8") class Engine: def __init__(self, displacement): self.displacement = displacement my_car = Car("Black", "Sedan") print(my_car.engine) print(my_car.engine.displacement)
2 Likes

Hi, thank you for responding. Correct me if I’m wrong below, just tryna understand your explanation.

So this part my_car = Car("Black", "Sedan"), this entire part is called object instantiation, right? Okay, and the type of my_car object is now a class because of the instantiation, and the class type is Car because the class name is Car, okay. I don’t get the __init__ part, I thought it’s a method and only helps initialize the attributes of the class Car, or does it mean that when the program runs, __init__ initializes the instance variables (color and style) and these instance variables are only specific to my_car?

So do I call self.color an instance variable? Because the variables color and style are in the “instance of the class”, I guess it’s called the instance variable, right? Or can I just call them instances?

my_car is an instance of Car with variables my_car.color and my_car.style
my_car is an object of class Car with variables my_car.color and my_car.style

Why isn’t my_car called an instance variable then? my_car is a variable isn’t it?
Does it mean that my_car is an object because the class Car has a group of objects and that we are assigning those group of objects to my_car and hence, we can refer my_car as an object, it’s just that we need to explicitly say it is "of class Car?

So that means this entire thing here can also be called an object? It has an instance method and instance variables.

class Car:

    def __init__(self, color, style):
        self.color = color
        self.style = style

It might help if you double check any terms in the glossary as it is easy to get tripped up by the terminology as much as anything else.
https://docs.python.org/3/glossary.html

For a lengthy but useful introduction to classes (the bit about namespaces and attributes may be especially helpful here)-
https://docs.python.org/3/tutorial/classes.html

I think __init__ was covered in your other question but to try and make it clearer- As mentioned above your class is a template for creating new objects, __init__ is designed to add attributes to new instances created from your class not to add attributes to the class itself.

I think you’re definitely tripping up over the definition of object here. The syntax isn’t the object. When the class Car: ... code block is executed it creates a class object.

The basic purpose (but not only purpse) of your class object is to create new objects. When you run Car("color", "style") you create a new object from your class. It is common to create multiple objects from the same class. Your __init__ method ensures each of these objects has useful attributes bound to it. Those attributes are normally unique to the instance.

For example we use Car to create multiple cars-

car1 = Car("black", "sedan")
car2 = Car("yellow", "sports")

The __init__ method takes the arguments you pass and in this case you have assigned them directly as attributes of your new instance objects.

Now we can use those attributes in each instance we create from our class (every instance here has different attributes)-

print(car1.color, car1.style)
print(car1.color, car2.style)
3 Likes

my_car is more than a variable, currently it only has 2 variables defined in it but typically it would have methods associated with it. It’s like calling your car a recliner chair, it does have a chair in it, but it’s a mischaracterization of what you use the car for. Having a class and instance variables allows you to write simpler methods contained in the class that can be reused. This concept becomes clearer as you learn more and make more involved classes. You could add instance variables for a gas tank, mileage, tire wear, windshield wiper fluid, oil, car weight, and methods for filling the tank, driving places, adjusting weight based on cargo and/or passengers in the vehicle, track gas tank levels with average mpg and adjustments based on current weight, or whatever else you might think of. All of those variables would be contained in the object my_car, with that class’s methods available to that object. If you wanted to make a new car, my_wifes_car = Car("White", "SUV"), that would have all those instance variables and the methods to act on them. So my_wifes_car.color = "White" while maintaining the variables of my_car.
my_car.get_gas(5) would put 5 gallons into my_car without doing anything to my_wifes_car (assuming you created self.gas_tank and a def get_gas(self, amount):method to fill it)

Some of the wording is semantics, though semantics might be important in the programming world. my_car.color and self.color are instance variables, the first is defined and contained in the object and thus I end up thinking it’s a variable and an instance variable. The second is like a parameter of the class method __init__, you call it an instance variable but since it inherently has no value it doesn’t feel right to call it a variable.

Hopefully I’ve wrapped my head around this the right way. :laughing:

1 Like

Okay, I will try to show my understanding from the answers which both of you have kindly provided:

class Car: # Name of class

    def __init__(self, color, style): # This method initializes the attributes of the class Car with the self parameter as the instance of the class, which in this case is my_car

        # Instance Variable / Instance Attributes
        self.car_color = color # The value of color will be "Black" 

        # Instance Variable / Instance Attributes
        self.car_style = style # The value of style will be "Sedan"
    
# my_car is now an object of the class Car
my_car = Car("Black", "Sedan") # Instantiate the class Car and passing the arguments "Black" and "Sedan" into the __init__ method

Please correct me if I’m wrong in any part of my understanding…thank you

1 Like