Mutable, Immutable… everything is object!

Ahmed Omar MILADI
4 min readJan 17, 2020

Object-oriented programming (OOP): Is a programming paradigm based on the concept of “Objects” , which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). A feature of objects is an object’s procedures that can access and often modify the data fields of the object with which they are associated (objects have a notion of “this” or “self”). In OOP, computer programs are designed by making them out of objects that interact with one another. OOP languages are diverse, but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types.

Id and type in Object-oriented programmig(OOP):

Id: An identity in object-oriented programming, object-oriented design and object-oriented analysis describes the property of objects that distinguishes them from other objects. This is closely related to the philosophical concept of identity.

Type: Python have a built-in method called as type which generally come in handy while figuring out the type of variable used in the program in the runtime.

type(object)
type(name, bases, dict)

Input:

x = 5

s = "type"

y = [1,2,3]

print(type(x))

print(type(s))

print(type(y))

Output:

class 'int'
class 'str'
class 'list'

f you need to check type of an object, it is recommended to use Python isinstance() function instead. It’s because isinstance() function also checks if the given object is an instance of the subclass.

Mutable objects :

In object-oriented and functional programming, an immutable object (unchangeable[1] object) is an object whose state cannot be modified after it is created.[2] This is in contrast to a mutable object (changeable object), which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object’s state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

Strings and other concrete objects are typically expressed as immutable objects to improve readability and runtime efficiency in object-oriented programming. Immutable objects are also useful because they are inherently thread-safe.[2] Other benefits are that they are simpler to understand and reason about and offer higher security than mutable objects

Immutable objects :

In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

Strings and other concrete objects are typically expressed as immutable objects to improve readability and runtime efficiency in object-oriented programming. Immutable objects are also useful because they are inherently thread-safe. Other benefits are that they are simpler to understand and reason about and offer higher security than mutable objects

why does it matter and how differently does Python treat mutable and immutable objects ?

  • Immutable objects are objects that cannot be modified. For example, numbers, strings and tuples are immutable objects.
  • Mutable objects are objects that can be modified. For example, lists and dictionaries are mutable objects.

Since immutable objects cannot be modified, when modifying variables that refer to immutable objects, variables always refer to other objects.

how arguments are passed to functions and what does that imply for mutable and immutable objects?

Arguments are always passed to functions by reference in Python. The caller and the function code blocks share the same object or variable. When we change the value of a function argument inside the function code block scope, the value of that variable also changes inside the caller code block scope regardless of the name of the argument or variable. This concept behaves differently for both mutable and immutable arguments in Python.

In Python, integer, float, string and tuple are immutable objects. list, dict and set fall in the mutable object category. This means the value of integer, float, string or tuple is not changed in the calling block if their value is changed inside the function or method block but the value of list, dict or set object is changed. Consider the mutable and immutable as states of the function arguments in Python. Let’s discuss it in detail with examples in the following section.

--

--