Everything is object

am2701
3 min readMay 26, 2021

Introduction

Things to know in python is: everything is object (Yes! My title was a clue).

Python is a smart language. It optimizes resources by making two names that refer to the same string value refer to the same object.

Id and type

Python id() function returns an identity of an object. This is an integer which is guaranteed to be unique. This function takes an argument an object and returns a unique integer number which represents identity. Two objects with non-overlapping lifetimes may have the same id() value.

The id is the object’s memory address, and will be different for each time you run the program. (except for some object that has a constant unique id, like integers from -5 to 256)

Input:

# Python id() function example
# Calling function
val = id("Javatpoint") # string object
val2 = id(1200) # integer object
val3 = id([25,336,95,236,92,3225]) # List object
# Displaying result
print(val)
print(val2)
print(val3)

Output

139963782059696
139963805666864
139963781994504

Type function return the type of the object you give as parameter to the function.

Input:

a = 1
print(Type(a))

Output:

int

It is mostly recommended to use this function to insure you to have the recommended type for a variable in setters functions.

What does mutable object means ?

To make it easy to understand, you could see this notion like that:

Object are updatable after creation

Mutable type examples:

list, dict, set, byte, array

A funny example:

If you would print the result of an increment string with a word every time you call it to have a result like that:

String
String, String
String, String, String
String, String, String, String

you just need to make a main to call a function magic_string for n time like that:

for i in range(10):
print(magic_string())

To explain this, you have to understand the default parameter initialization. You can define the value of a parameter if you don’t pass it. When you type prmList=[] you define the value of prmList if you don’t give it by an empty list.

def magic_string(prmList=[]):
prmList += ["String"]
return ", ".join(prmList)

The funny thing with this function and python is, list is mutable object and you don’t give parameter in your main but the function will initialize first time a mutable variable and will re-use it for the second call (but with your amend).

Then and immutable object ?

And reverse to previous notion, you could see it like that:

Object are not updatable after creation

Immutable type examples:

int, float, string, tuple

Because an example is better than speech:

Just imagine a function to increment an integer:

def increment(prmInt):
prmInt += 1
return prmInt

If you use it like that:

a = 1
increment(a)
print(a)

The output wille be:

1

Why ? You will tell me !

Because there is not link between a variable and prmInt parameter. The variable is not udpatable. The variable a, because of his type, is not mutable. So, the variable a is immutable.

To have an incrementation of the variable a you should do:

a = 1
a = increment(a)
print(a)

And then, you will have:

2

--

--