Object References & Object Identity – Variables in Python

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z! Let’s start with Object References in Python…

🖥️ Variables in Python: Object References and Object Identity

What is actually happening, when you make a variable assignment? This is an important question in Python, because the answer differs somewhat from what you would find in many other programming languages!

Python is a highly object-oriented language. In fact, virtually every item of data in a Python program is an Object – of a specific type, or class. This point will be reiterated many times, over the course of these tutorials.

print(300)

Consider this code. When presented with the statement print(300), the interpreter does the following steps:

– Creates an integer object
– Gives it the value 300
– Displays it to the console

You can see that an integer object is created, using the built-in type() function.

type(300)
OUTPUT: class 'int'


A Python variable is a symbolic name, that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object using that name. But, the data itself is still contained within the object.

○ Variable Assignment

n = 300

For example. This assignment creates an integer object, with the value 300, and assigns the variable n to point to that object.

The following code verifies that n points to an integer object:

print(n)
OUTPUT: 300

type(n)
OUTPUT: class 'int'



○ Multiple References to a Single Object

Now consider the following statement:

m = n

What happens, when it is executed? Python does not create another object. It simply creates a new symbolic name or reference m, which points to the same object that n points to.


○ References to Separate Objects

Next, suppose you do this:

m = 400

Now Python creates a new integer object with the value 400, and m becomes a reference to it.


○ Orphaned Object

Lastly, suppose this statement is executed next:

n = "foo"

Now Python creates a string object with the value “foo” and makes n reference that. There is no longer any reference to the integer object 300. It is now orphaned and there is no way to access it.



Tutorials in this series will occasionally refer to the lifetime of an object. An object’s life, begins when it is created. At which time, at least one reference to it is created. During an object’s lifetime, additional references to it may be created, and references to it may be deleted as well. An object stays alive, as long as it has at least one reference to it.

When the number of references to an object drops to zero, it is no longer accessible. At that point, its lifetime is over. Python will eventually notice that it is inaccessible and reclaim the allocated memory, so it can be used for something else. In computer lingo, this process is referred to as “garbage collection”.

Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy

***

Copy link
Powered by Social Snap