Object References & Object Identity - Variables in Python - Python Tutorial for Beginners

Publicado em: 06 Fevereiro 2021
no canal de: Digital Academy
2,442
65

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ 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 in Python

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 in Python

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 -    • DATA TYPES in Python (Numbers, String...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assi...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF,...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Co...  

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

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] Python for Absolute Beginners: http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#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/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***


Nesta página do site você pode assistir ao vídeo on-line Object References & Object Identity - Variables in Python - Python Tutorial for Beginners duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Digital Academy 06 Fevereiro 2021, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 2,442 vezes e gostou 65 espectadores. Boa visualização!