Python3, Everything is object!

Issam Sebri
4 min readMay 26, 2020

Photo by Semen Borisov on Unsplash

Introduction

Everyone knows what’s an object, it’s a “thing” we can feel it and we can sense. In computer science not much different coming to data, they are “things” we can carries, transfers, stores and even deletes them. In Python there is a very known slogan that says everything is object which mean number, string, function is an object which mean they are all instance of some “Class” and almost everything has his own attributes and methods even a simple number or simple character. Here a simple track behind a simple number “1

We can see clearly see how much a simple number like “1” have that much of methods and attributes and there is the same amount or may be more for other type of data and objects. but we need to figure out the paradigm organization behind the scenes and how python manage this object.

Type and ID(entification)

So beginning from when we stop in the introduction, I can tell you that we can create a simple empty object named by “x” using the built-in function “object()” and we got the next result:

So as we know x is an empty object, and by looking to the example we see that python create for x an identifier a unique number for it. this number is very important for the inner mechanism of python is the main way to handle assignment, equity, and comparison between object and attributes.

In order to retrieve an id of object we use the built-in function “id” that return the identity of an object. which allow us to say that the ‘x’ is a pointer to a memory spot and the “id” is the memory address of this spot:

Coming to the object type we see that “object” -we see it in our example- is the most based data type. and every object has type which mean that every object is an instance of such Class type.

The type precise the amount of available attributes and methods for each object. all instance of that Type class share the same feature of it. Really you should think Python for this magic especially if you know you could make your own types and Class and you can get benefit of this features.

Mutable objects

Mutability in Python is the fact that objects is manipulable and we can change his state after birth. A mutable object can change his state without changing his identity (id).

Also mutability is crucial benefit for aliases variable, when more than one alias (variable) refer to the same object is be easy to manipulate the same object from different place.

Immutable objects

Immutable objects is persistent against change every try to change his state let the python interpreter complain:

But unfortunately there is a way to trick an immutable object by mixed with a mutable one to go behind his rules.

Python immutable and mutable objects

Mutable and immutable objects are handled differently in python. Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy. Whereas mutable objects are easy to change.

Use of mutable objects is recommended when there is a need to change the size or content of the object.

As a rule of thumb, Generally Primitive-like types are probably immutable and Customized Container-like types are mostly mutable.

Function mechanism in python

In general arguments call differ from programming language to another but to summarize techniques there is two main concept “Call by value” and “Call by reference”.

  • Call by value: is the most used strategy and is more known for C/C++ programming language. is considered to evaluate the passed expression and copied to a parameter.
  • Call by reference: Is the fact that the function take an implicit reference to the variable (a pointer) rather than copied it. which can cause a serious bug with malicious use of variable, Used mainly in Perl programming.

What about Python? Python actually mix the two strategy, which is a third technique known as “Call-by-object” In other words, Python initially behaves like call-by-reference, but as soon as we change the value of such a variable, i.e. as soon as we assign a new object to it, Python “switches” to call-by-value. That is, a local variable x will be created and the value of the global variable x will be copied into it.

If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can’t be changed within the function, because they can’t be changed at all, i.e. they are immutable. It’s different, if we pass mutable arguments. They are also passed by object reference, but they can be changed in place within the function. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place, i.e. the list will be changed even in the caller’s scope. If a new list is assigned to the name, the old list will not be affected, i.e. the list in the caller’s scope will remain untouched.

In other words, Python initially behaves like call-by-reference, but as soon as we change the value of such a variable, i.e. as soon as we assign a new object to it, Python “switches” to call-by-value. That is, a local variable x will be created and the value of the global variable x will be copied into it.

Reference: All the credit for this resource for helping me understand a beautiful things about Python:

And of course all my thanks to ‘Ines Chokri” for her time reviewing my post.

Sign up to discover human stories that deepen your understanding of the world.

Issam Sebri
Issam Sebri

No responses yet

Write a response