Desbaratamos un laboratorio narco

Como resultado de una gran investigación de la Policía de la Ciudad, se desbarató a una peligrosa banda narco que fabricaba drogas sintéticas para luego distribuirlas en fiestas electrónicas. La…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




ID and TYPE

Python newcomers first learn that everything in Python is an object and that some are either mutable or immutable. To start off, what is an object? In Python, when you want to create a new “thing”, you create a class. An object is a basic version of that class, or specifically known as an instance. So while a class is a blueprint, anything built from that blueprint are considered objects.

So “Everything in Python is an Object” means every variable holds an object instance. When an object is created, it is assigned a unique object id. Depending on the type of the object, a mutable object can be changed after it is created, and an immutable object can not be changed.

Objects of built-in types like (int, float, str, tuple) are immutable while objects of built-in types like (list, set, dict) are mutable. Custom classes are generally mutable but can be set up to simulate immutability.

To find out if our variable is a mutable or immutable object, we need to use the id() and type() function.

The function id() returns the identity of an object as an integer. This integer can be understood as a object’s location in memory. Each object has a unique id and can be compared to other objects. One can compare if two variables reference the same objects based on their ids. The is operator compares the identity of two objects.

The built-in function type() returns the type of an object. Here is a simple example.

Using both functions, we can demonstrate how different types of objects are associated with variables and its mutability.

A mutable object can change its state or contents. Here is an example:

We just created an object of type int (12). Identifiers a and b both point to the same object as their ids are the same.

If we do a simple operation as below. We see a is no longer pointing to same object as b.

The object in which a was tagged is changed to another object. The object 12 we created was never and cannot be modified hence the object 12 of type int is an immutable object.

Lets switch gears and look in the case of mutable objects:

Here we created an object the type list with variables x and y pointing to the same object, which is a list of 4 immutable objects.

Removing an item from list with the pop() function changes the object,

If we check the id on both identifiers, x and y will be pointing to the same modified object.

However, Python allow some immutable objects to have some mutability properties. An example is the object of the type tuples.

Let us consider a tuple = (‘Bill’, [1, 2, 3]). The tuple contains an immutable string and a mutable list.The tuple itself isn’t mutable since we can change the contents. The same goes for the string since we don’t have the ability to change it. However, the list object does have mutating methods, so it can be changed.

Python treat mutable and immutable objects differently due to their properties. Immutable are quicker access but are expensive to change because doing so required creating a clone of that object. Immutable also ensure the object is free from any change or tampering. On other hand, mutable objects are needed because users can make changes in-place, without allocating a new object. Just remember whenever you make an in-place change to an object, all references to that object will now reflect the change.

Lets dive into how objects are pass into functions. Here is an example of immutable object pass into a function:

As we can see from the above example, we have called the number via call value. With a = 3, we have the variable referring to value 3 of int type and when we passed it to increment function, the local variable n points to teh same object int 3. But int 3 is immutable, so we have to create a new object of value 4 with variable n pointing to that new object. Identifier, a, is still pointing to object int 3.

So can we ever manipulate immutable objects when passing them into functions? We can by reassigning the variable, a, to point to our new return value of 4.

In the above example the same object is passed to the function, but the variables value doesn’t change even though the object is identical. This is called pass by value. So what is exactly happening here? When the value is called by the function, only the value of the variable is passed, not the object itself. So the variable referencing the object is not changed, but the object itself is being changed but within the function scope only. Hence the change is not reflected.

In the case of passing mutable objects:

Here we have called the list via call by reference, so the changes made to the original list is possible. Since no new object is created, there is no need to reassigned identifier, n, to point to the modified list.

Add a comment

Related posts:

On growing up

Last Monday I was on an early morning itinerary from San Jose Costa Rica to Mexico City, and finally to Vallarta. Flight was leaving at 6:30am, and I was checked in and waiting by 4:00am. Because…

How to Overcome Mental Fatigue

Whether you live in America or elsewhere globally, I’m sure you’re experiencing fatigue from the 2020 US Presidential Election. Combined with fatigue from the global pandemic, lockdowns…

Importance Of STEM Education

From accessing all kinds of information on the internet to the launch of new intuitive smart devices, technology has always been a cornerstone of many of humanity’s successes. Technology is…