Introduction
Python is an object-oriented programming language (OOP) and, maybe you don't know it yet, but in Python everything is an object! 📦
It may seem strange, but it is true and in this article we will prove it! 👩🎓
First of all, let's keep in mind that:
- every variable is a pointer
- a variable has no type information
what is a pointer : Imagine your computer's memory as a large set of numbered boxes, each with a number (the address). A normal variable stores a value, such as a number or a character, in one of these boxes. A pointer variable, on the other hand, does not store a direct value, but stores the number of the box (the address) where the value of another variable is located.
So, if you have a pointer variable p that points to x, you can use p to access the value of x indirectly, without directly touching x. This is useful in many situations, such as passing data between functions without making copies or dynamic memory management.
Python - Everything is an Object
Point 2 (described in the Introduction) often leads to the belief that Python is a type-free language, but this is not the case. Let's see it with an example:
x = 4
type(x) #int
x = 'hello'
type(x) #str
Types are not directly linked to the variable but to the objects themselves.
The phrase “in Python everything is an object” means that every language construct has :
- metadata (called attributes)
- functionalities (called methods)
Indeed:
x = 41.75 #this number has a meaning for me, try to find out. I await your comments ^_^
x.is_integer() #False
Wait a minute, but if everything is an object then attributes and methods …?
Well yes, they are objects too 😲:
type(x.is_integer) # builtin_function_or_method
Let's go back to point 1: every variable is a pointer… Hold on tight because what you are about to discover will surprise you 🎁
We create two variables x
and y
:
x = [5, 18, 90] #just three random numbers, maybe try playing them in the lottery :D
y = x
Since every variable is a pointer, both x
and y
point to the same object in memory and this has some consequences. Let's see them with an example:
print(y)
x.append(14)
print(y)
Can you imagine what the output of our example will be? Here it is:
Output
[5, 18, 90]
[5, 18, 90, 14]
This happens because the two variables are pointing to the same memory area, so changes made to one variable affect the other.
You're probably feeling confused now, but don't worry: writing python code is still as easy as it was before reading this article 😊
In fact, if you are wondering if normal operations can have side effects like the one seen above, the answer is no. Here are some examples to avoid staying awake at night 😉:
x = [5, 18, 90]
y = x
x = 'something else'
print(x)
print(y)
x = 1
y=x
x += 5
print(f'x:{x}')
print(f'y:{y}')
y=100
print(f'x:{x}')
print(f'y:{y}')
Output
something else
[5, 18, 90, 14]
x:6
y:1
x:6
y:100
Follow me #techelopment
Medium: @techelopment
Dev.to: Techelopment
facebook: Techelopment
instagram: @techelopment
X: techelopment
telegram: @techelopment_channel
youtube: @techelopment
whatsapp: Techelopment