Mutable And Immutable Objects in Python.

Angui Clavijo Gutiérrez
3 min readJun 10, 2021

This Posted you learned about language of programming, in this topics:

  • That is id and type
  • The mutable objects
  • The immutable objects
  • Why is important this objects
  • Differently this objects
  • How arguments are passed to functions and that imply for mutable and immutable objects.

In python the objects is assigned a unique identification number when it is he operator stored in memory this can be fetched by id( ) function.

The is operator compare if two id( ) of two objects and returns True if both objects have same value otherwise it return False.

The is not operator return False if id( ) of objects is same and True otherwise.

Python defines variety of data type of objects, that this objects is stored in memory and object mutability depends of type, for example:

  • The Lists and Dictionaries is Mutable, that is to say that change their contents but not change their identity. This example we are created a list, and access to index zero with value of ‘1’ and assigned to value of ‘a’, this refer mutable, can be modified.
  • The Floats, Int, Strings and Tuples have no provision to change there assigned value for an index. The Tuple is a collection which ordered and unchangeable, it does not allow duplicate members, the Tuples is write round ( ), its separate for commas.
    This example the tuple is ordered and unchangeable that is to say cannot be modified, the assigns in the index zero for value ‘50’ is created but no is execute, return a error that indicate is immutable (cannot be modified)

The immutable objects are quicker to access and are expensive to change because it involves the creation of a copy whereas mutable objects is are easy to change.

Use of mutable objects is recommended when there is need to change the size or content of the object.

How work NSMALLPOSINTS and NSMALLNEGINTS, It is actually an array of 262 integers (most commonly used). And this structure is basically used to access these integers fast. They get allocated right when you initialize your NSMALLPOSINTS and NSMALLNEGINTS.

what is Frozent Sets:

Frozen sets are a native data type in Python that have the qualities of sets including class methods but are immutable like tuples.

To use a frozen set, call the function frozenset() and pass an iterable as the argument and if you pass another data type such as a list, tuple, or string, then it will be treated as an iterable.

--

--