Sitemap

PYTHON3 -EVERYTHING IS OBJECT

3 min readJun 2, 2022

--

Id and type, mutable and immutable objects

Python has been an object-oriented programming language since its existence.

Everything in Python is an object, and almost everything has attributes and methods. All functions have a __doc__ attribute that returns the documentation string defined in their source code. The sys module is an object that contains (among other things) an attribute called path. And so with everything.

Id() and Type()

The built-in Python id() function returns an integer representing the identity of the given object. Each object in a program, during program execution, is assigned a unique integer for an object. This identification may change from one running instance of the program to another.

Sintax: id(object)

Press enter or click to view image in full size

Python type() is a built-in function that helps you find the class type of a given variable as input. You just put the variable name inside the type() function, and python returns the data type.

Sintax: type(object)

Press enter or click to view image in full size

Mutable and Immutable object

The value of some objects can change. An object whose value can change is called a mutable object; An object whose value cannot be changed once created is called an immutable object.

(The value of an immutable container object that contains a reference to a mutable object can change when the value of the mutable object changes; however, the container is still considered immutable because the collection of objects it contains cannot change. Therefore, Immutable is not strictly the same as having an immutable value, it’s more subtle).

An object’s mutability is determined by its type; for example, numbers, strings, and tuples are immutable, while dictionaries and lists are mutable.

Get santiago osorno’s stories in your inbox

Join Medium for free to get updates from this writer.

Mutable Objects: Here are the objects in Python that are of mutable type:

  1. list
  2. Dictionary
  3. Set
  4. bytearray
  5. user defined classes

Immutable Objects: Here are the objects in Python that are of immutable type:

  1. int
  2. float
  3. decimal
  4. complex
  5. bool
  6. string
  7. tuple
  8. range
  9. frozenset
  10. bytes

How arguments are passed to functions and what does that imply for mutable and immutable objects.

Immutable Python objects (such as numbers, tuples, and strings) are passed by reference, as are mutable objects (such as lists, sentences, and dictionaries). Due to the state of immutable (immutable) objects, changing an integer or string value within a function block behaves like copying an object. Creates and manipulates a new local copy of the calling object within the scope of the function block. The caller object remains unchanged. Therefore, the calling block does not notice any changes made to immutable objects within the scope of the function block.

— Thanks for your reading.

--

--