Python Objects

Python is an “object-oriented programming language.” What it means is that basically everything in Python is an object. Methods and lists, variables and strings are objects.

Id() and Type() Built-In Functions

id() built-in function in Python returns the “identity” of the object or it’s unique number. The id is almost like an address of the object.

for example if you declare the object like this:

Note, that even if declare a new variable, but set it again equal to the same integer (for example) as the variable before — the id will be the same as the first variable declared. Python takes the object after the equal sign:

Type() is another built-in function in Python that lets you identify what type is the object.

Let’s see what is the type of our a object declared earlier:

We now can see that the ‘a’ is the type — Integer

Mutable vs Immutable Objects

It’s important to know that objects in Python are not behaving the same way. Different objects react to changes differently. Here is where the specifications of mutable vs immutable objects is coming into play:

  • mutable objects can be changed, altered
  • immutable objects can’t be changed, and instead they will return a new object if updated.

Here is a list of some mutable objects:

  • list
  • dictionary
  • set
  • class

Immutable objects:

  • int
  • float
  • tuple
  • string
  • bool

let’s see example of trying to alter immutable object:

we declare a string word = “Hello”

if we try to alter the string stored in “word” and add extra string to it — we notice that id(word) had changed — Python had created a new object to store updated string:

Let’s do the same with the mutable object — list:

we see that even after adding an element to the list, the id(list) stayed the same — so Python “updated” the same object and didn’t use any resources to create a new one.

Why does it matter if you are using mutable vs immutable objects:

  • using a lot of strings (or other immutable objects) and trying to alter them — will result in more memory usage — since with every change a new object will be placed in memory

Passing arguments to functions

There are two ways to do that — call by value and call by reference

  • pass-by-value — is the most common way to pass argument — just send a value to the function by copying the value. When passing immutable objects this way — we are safe, since the copy of the object is created, and the object itself is not changed in any case when using function.
  • pass-by-reference — function takes a reference to the argument without making a copy of it. By using this passing way — system saves on memory space and processing time. When using this method — it is important to keep track of what arguments are being passed (mutable or immutable) — so that the value of the argument is not “accidentally” changed during function usage.

Integer objects in Python

Interesting fact about certain range of integers in Python.

if we are to type this a = 2 — no new object is created in Python. It is because integers in range from -5 to 256 are already preloaded in Python, and by assigning a variable to the int in this range only creates a reference to the already existing object.