Everything is an Object in Python

jassem ben ali
3 min readOct 5, 2021

--

Introduction:

Python is a general-purpose, versatile, and powerful programming language. It’s a great first language because it’s concise and easy to read. Whatever you want to do, Python can do it. From web development to machine learning to data science, Python is the language for you.

In Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in programming. It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, etc. in the programming. The main concept of OOPs is to bind the data and the functions that work on that together as a single unit so that no other part of the code can access this data.

Main Concepts of Object-Oriented Programming (OOPs): Class, Objects, Polymorphism, Encapsulation, Inheritance

id and type:

Python id() function returns an identity of an object. This is an integer which is guaranteed to be unique. This function takes an argument an object and returns a unique integer number which represents identity. Two objects with non-overlapping lifetimes may have the same id() value.

Type

Type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three argument. If single argument type(obj) is passed, it returns the type of given object. If three arguments type(name, bases, dict) is passed, it returns a new type object.

Mutable, Immutable…:

Python has two natures of objects: mutable and immutable.

The value of mutable objects can be changed after they are created. The value of immutable objects cannot be.

A list is a mutable object. You can create a list, append some values, and the list is updated in place.

A string is an immutable object. Once you create a string, you can't change its value.

When you change a string, you’re actually rebinding it to a newly created string object. The original object remains unchanged, even though it’s possible that nothing refers to it anymore.

Even though we’re using += and it seems that we're modifying the string, we really just get a new one containing the result of the change.

An immutable variable cannot be changed after it is created. If you wish to change an immutable variable, such as a string, you must create a new instance and bind the variable to the new instance. A mutable variable can be changed in place.

Caveat: A tuple, which is immutable, can contain mutable elements such as a list ex: (1, 2, [3, 4, 5]) . The reference to the list within the tuple can remain unchanged, even if the contents of the list are modified.

--

--