In this article I will be talking about what the id and type functions are and how to use them. Also I will be talking about mutable and immutable objects.
Python offers some tools to find out whether an object is mutable or immutable through, the functions id( ) and type( ).
id and type functions
id function: Type( ) it’s the function we use to print the type of an object.
>>> a = 47
>>> b = a
>>> id(a)
7904
>>> id(b)
7904
>>> a is b
True
Type function: with id( ) fucntion we can get the variable identifier (which is the memory address in the CPython implementation).
>>> a = 47
>>> b = a
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>
Mutable objects
In Python, some common mutable objects include lists, dicts, and sets. In simple terms, when an object is referred to as being mutable, it means that the object, or the contents of that object can be altered.
Immutable objects
Common immutable objects in Python include ints, floats, tuples, and strings. Unlike mutable objects, the immutable objects can’t be changed.
How NSMALLPOSINTS and NSMALLNEGINTS work?
It is actually an array of 262 integers . And this structure is basically used to access these integers fast. They get allocated right when you initialize your NSMALLPOSINTS and NSMALLNEGINTS.
In Cpython the intigers in the range (-5 to 256) those values are used, because they are most used integers. This is a structure to access faster to those numbers, so there is no need to create them.
Top comments (0)