Jo Pinta
3 min readJun 1, 2021

--

When Guido Van Rossum designed the lenguage he wrote: “One of my goals for Python was to make it so that all objects were first class”. So all objects have equal status, like they can be assigned to variables , placed in lists or passed as arguments everithing is treated the same way, everithing is a class.

Id and Type

Becasue everything is treated as a class, when type() is entered it return the class, i.e <class ‘int’> . There is not a native type meaning all are objects. When id() is typed is to know where the object is stored in memory. it’s a decimal value. To have it in hexadecimal you can do hex(id()).

Mutable and Immutable objects

Whether an object is mutable or not depends on its type. The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. Some types such as numbers, strings and tuples are immutable, they are always the same. Dictionaries and lists are mutable. Tuples are immutable lists , frozensets are immutable sets.

>>> a = [1,2,3]
>>> a.append('hello')
>>> a
[1, 2, 3, 'hello']

As shown in the example above list ‘a’ has mutated but is still the same object in the same memory position. The list object has the append method, which will actually mutate the object.

An other example is when excecuting assigment statements,

a = “banana”

b = “banana”

In one case, a and b refer to two different things that have the same value. In the second case, they refer to the same thing.

to test if two names have the same value,use ==

>>> a == b
True

To test if two names refer to the same object use the ‘is’ operator

>>> a is b
True

If we assign one variable to another,

Both variables refer to the same object

The same list has two different names, it is aliased. Changes made with one alias affect the other

Because strings are inmmutable, the interpreter optimizes resources and referthem to the same object. but with lists is diferent,

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True
>>> a is b
False

Python treat mutable and immutable objects differently, they are stored in parts or memory that you can modified or not, If so it will require the creation of a copy, in where it will change in a easier way.

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True
>>> a is b
False

That is, when you call a Python function, each function argument becomes a variable to which the passed value is assigned. All Python objects are implemented in a particular structure. One of the properties of this structure is a counter that keeps track of how many names have been bound to this object.

Everything is a class in Python. Thank you and keep coding.

--

--