Confused about object, class, instance,

I totally lost in this chapter. Can someone help explain what is the difference between object and variable in a super simple example?

Thank you a lot!!!

Hi @candiceliu93
In addition to the explanation in chapter itself may be this will help you understand more about the two. and then you can read the explanation below.

As you’ve seen in many time , when you run an assignment statement such as a = 3 in Python, it works even if you’ve never told Python to use the name a as a ” “variable, or that a should stand for an integer-type object. In the Python language, this all pans out in a very natural way, as follows:
Variable creation
A variable (i.e., name), like a, is created when your code first assigns it a value. Future assignments will change the value of the already created name. Technically, Python detects some names before your code runs, but you can think of it as though initial assignments make variables.
Variable types
A variable never has any type of information or constraints associated with it. The notion of type lives with objects, not names. Variables are generic in nature; they always simply refer to a particular object at a particular point in time.
Variable use
When a variable appears in an expression, it is immediately replaced with the object that it currently refers to, whatever that may be. Further, all variables must be explicitly assigned before they can be used; referencing unassigned variables results in errors.
In sum, variables are created when assigned, can reference any type of object, and must be assigned before they are referenced. This means that you never need to declare names used by your script, but you must initialize names before you can update them; counters, for example, must be initialized to zero before you can add to them. This dynamic typing model is strikingly different from the typing model of traditional languages. When you are first starting out, the model is usually easier to understand if you keep clear the distinction between names and objects. For example, when we say this to assign a variable a value:
>>> a = 3 #Assign a name to an object

at least conceptually, Python will perform three distinct steps to carry out the request. These steps reflect the operation of all assignments in the Python language:
1. Create an object to represent the value 3.
2. Create a variable a if it does not yet exist.
3. Link the variable a to the new object 3.
The net result will be a structure inside Python that resembles Figure 6-1. As sketched, variables and objects are stored in different parts of memory and are associated by links (the link is shown as a pointer in the figure). Variables always link to objects and never to other variables, but larger objects may be linked to other objects (for instance, a list object has links to the objects it contains).

Untitled picture

These links from variables to objects are called references in Python—that is, a reference is a kind of association, implemented as a pointer in memory.[15] Whenever the variables are later used (i.e., referenced), Python automatically follows the variable-to-object links. This is all simpler than the terminology may imply. In concrete terms:
Variables are entries in a system table, with spaces for links to objects.
Objects are pieces of allocated memory, with enough space to represent the values for which they stand.
References are automatically followed by pointers from variables to objects.

At least conceptually, each time you generate a new value in your script by running an expression, Python creates a new object (i.e., a chunk of memory) to represent that value. As an optimization, Python internally caches and reuses certain kinds of unchangeable objects, such as small integers and strings (each 0 is not really a new piece of memory—more on this caching behavior later). But from a logical perspective, it works as though each expression’s result value is a distinct object and each object is a distinct piece of memory.
Technically speaking, objects have more structure than just enough space to represent their values. Each object also has two standard header fields: a type designator used to mark the type of the object, and a reference counter used to determine when it’s OK to reclaim the object.

Hope it helps!
Happy Learning!

Excerpt From: Mark Lutz. “Learning Python”
1 Like

thank you @harsh.raizada… it is a lot. I think i need time to digest it. So what we do for creating a class? what is use for?

Hi @candiceliu93: A class is like a template where you create instances of the class (objects) from. An example would be creating cookies from cookie cutters. Classes also contain Methods (functions in classes), which allow some action to be performed, like collecting/sorting the cookies and constructors which define the properties of the object (like the shape and length of the cookie). Note that a constructor is basically a special method that allow properties to be assigned to each individual object.

You can call methods of objects (defined in the class already) by using the dot syntax (e.g. cookie.calculateLength(), but for variables, they are usually used as an argument to be passed into the methods (e.g. calculateLength(shoesize)). When you define a variable, you are basically allocating some portion of memory to store the contents of the variable. Objects on the other hand are a collection of properties and methods.

Hope this clarifies!

1 Like

Thank you for explaining. I have a clear picture now after reading your answer. so if i need to use a function from a class, i need to use that format*’ object name.method that I def in class()’* just like you do cookie.calculenght(), cookie is the object. and object is instance. am I correct?

Hi @candiceliu93: Yes, cookie is the object (which also can be called the instance of the class). If my previous answer clarifies your doubts, do you mind marking it as the solution?