HYGH Attends Advertising Week London

Advertising Week Europe, held annually in London, is a global gathering of marketing and communications leaders, featuring unique media, marketing, technology and creative perspectives discussing key…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Everything is an object in Python

Now is time to talk about one of the most used languages in programming and also one of the easiest to learn due to its simple syntax. The content of this post is meant to be technical, as I will share how this language works under the hood. Python’s popularity is given not only because of its simple syntax, but its incredibly wide range of possibilities of usage, there exist loads of modules created to work in any area of development you can imagine, even videogames!

To begin with, Python is an object-oriented programing (OOP) language, meaning that its entire structure rounds around the object-oriented paradigm. This paradigm is based on the concept of “objects”, which can contain data and functions: data in the form of fields (often known as attributes or properties), and functions, in the form of procedures (often known as methods). The programs made under this paradigm are designed by making them out of objects that interact with each other. Many other languages besides python use them. So what makes python stand out from other languages is that everything is considered an object. Every data type that we request space of memory is an object. Yes, it sounds strange, but even an integer is considered an object to the python interpreter.

An object often is created to have methods that can modify the data fields of itself (in python this is possible using “self”). This gives the user the possibility of manipulating objects as is needed. Python is a class-based OOP language, meaning that objects are instances of classes, which also determine their types.

In Python to create an object, we need to create a class so our object can be instanced from it. We can think of a class as a template for objects with similar characteristics, objects will inherit them. Once we have declared our class we can instantiate an object from it. In python, objects are considered an abstraction for data. All data in a Python program is represented by objects or by relations between them.

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; it’s the object’s address in memory. to compare the identity of two objects we use the ‘is’ operator and the id ‘()’ function to get the integer representing the identity.

An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The ‘type()’ function returns an object’s type (which is an object itself). Like its identity, an object’s type is also unchangeable. Its type is defined by the class the object inherits. For example, the class of the object will be str, int (or a much more complex one we create), whereas the id will give us the address in memory of where the object is located.

The value of an object works a bit differently. The value of some objects can change and others don’t. Objects whose value can change are named mutable; whereas objects whose value is unchangeable once they are created are called immutable. this mutability is determined by its type. Numbers, strings, and tuples are immutable whereas lists, dictionaries, and sets are mutable.

For the last two, the type we can set, unset and modify its values freely but if we try to do it on the immutable ones the interpreter will give as an error.

We can define a list using square brackets ‘[ ]’ like this: numbers = [1, 2, 3]. To define a tuple, we just need to replace the brackets with parentheses ‘( )’ like this: numbers = (1, 2, 3). In both data types, we can access elements by index and we can iterate over them. The main difference is that a tuple cannot be changed once it’s defined.

However, if we want to expand these two data types we can do it with both. But a slightly different thing will happen. In order to do this, we use the ‘+=’ operator. Let see this example in the images below, ‘x’ is a list, and ‘y’ is a tuple. We started defining them, and then we extend their values. If we pay special attention to the id numbers we can see that when we assign new values to the tuple it id number changes. Now the tuple is a different object due to its immutability.

This same thing happens when we try to change the value of other immutable object types such as numbers(ints and floats) and strings. Its id number will change, it will be a different object but it will still be pointing to the same value. Python was built in C, so handling this low-level data works something like dealing with pointers, however, python does all the work for us. When we change a value of an immutable object a new object start is pointing to this value. That’s why its value doesn’t change while its object id does.

Now let’s specify how Python treats this type of data when they are passed to as an argument to a function. Immutable objects are passed by reference like mutable objects.

A function won’t be that useful if we pass an argument that we cannot change inside, so here is where the magic happens. Due to the state of immutable (unchangeable) objects, if an integer or string value is changed inside the function block then it much behaves like an object copying. A local new duplicate copy of the caller object inside the function block scope is created to be manipulated inside of the function. The caller object will remain unchanged. Therefore, the caller block will not notice any changes made inside the function block scope to the immutable object. It will be more clear in the example below.

Mutable objects like dictionaries and lists are also passed by reference. Let’s say a value of a mutable object is changed inside a function scope. As is a mutable object the value will change regardless of the name of the argument that was passed. Python in this case does not copy the values to another object as those in the previous example. The diagram below will clarify this concept.

It sounds a bit confusing how python manages objects and values at the beginning. However, when you start to use it for some programs you get used to it quickly and understand that most of the decisions made on how the language works behind the hood are for creating a more intuitive environment for the developer. Python is a great programing language and a must-learn for every developer.

Add a comment

Related posts:

How to Give Genuine Recognition

Recognition is one of the most powerful tools that any leader has in their arsenal. The desire to be recognised is a basic human need; we all want to be noticed for our individual efforts…

Ruby on Rails and How to Fight the Nested Resources Boss

Originally submitted in July 2020. This article is my Ruby on Rails project blog. I hope it is helpful and inspiring for you. Yes, this is how I called my Rails Project Blog title. So it feels like…

Six Reasons Why You Should Write For Yourself

Writing is an art. It may be a hobby to some, a profession to some, or an ardent source of joy and passion to some. With digital marketing, SEO and content-based writing on the boom, the art of…