Everything is object

Everything is object

INTRODUCTION

You probably know the following phrase "Everything in Python is an object". A thing? What is an object? We will define an object as a data abstraction. Python provides a variety of data types that can be handled as objects. Among them, we can find that strings, lists, functions and even modules are all objects. Now let’s see why everything is called an object in Python. Python has a quirk and treats its data type as mutable or immutable, which means that if the value can be changed, the object is called mutable, and if the value cannot be changed, the object is called immutable

No alt text provided for this image


ID :

The id() function receives an object as a parameter and returns another object, which serves as a unique identifier for the first object. The return value is an integer indicating the memory address of the storage object.

No alt text provided for this image


Sintax: id (object)

As mentioned above, python deals with mutable and immutable data types, and now we will better understand what the previous two expressions refer to.

No alt text provided for this image


Immutable objects

The type is immutable, meaning that it cannot be changed after it is created.

Immutable types

1. Numbers

2. String

3. Tuples

Let's look at an example

No alt text provided for this image


In this example, we have assigned the string "MAHDI" to the variable name. Then, we tried to change the second letter of the string, which gave us an error.

The error message is as follows: "The'str' object does not support item assignment"

This happens because we are trying to change the value of an immutable object, because strings in Python are immutable.

What we can do is to assign a new value to the variable, which is different from the changed string

No alt text provided for this image


It seems that we are changing the string, but in fact we are assigning a new value to it, because if you realize it, we will pass the = sign and the new value we want the variable to have.

Then you can use immutability to ensure that an object remains the same throughout the program

Mutable objects

Mutable object values can be changed anytime and anywhere after creation.

Mutable types

1.Lists

2.Sets

3.Dictionaries.

Let's look at an example

No alt text provided for this image


In this example, we assigned the characters 'M', 'A', 'H', 'D', 'I' to the list name. Then, we tried to change the second letter in the list, and this operation was successfully completed. This happens because we are trying to change the value of mutable objects, because lists in Python are mutable.

No alt text provided for this image


Why is it important, and how does Python handle mutable and immutable objects?

Python handles mutable and immutable objects in different ways. Access to immutable objects is faster than access to mutable objects. Similarly, immutable objects are fundamentally expensive to "change" because doing so involves creating copies. It is cheap to change objects that are mutable.

When you need to adjust the size of an object, a mutable object is great, for example, it will be dynamically modified

How to pass parameters to functions, and what does this mean for mutable and immutable objects?

Python Immutable Function Arguments

Immutable objects in python (such as numbers, strings, or tuples) are passed as parameters in the function, and they change within the function block, so their behavior is similar to a copy of the object. A new local copy of the calling object will be created and manipulated within the scope of the function block. The calling object will remain unchanged. Therefore, the calling block does not notice any changes made to immutable objects within the scope of the function block.

No alt text provided for this image


This value is only modified in the function block, outside the function, its value remains unchanged.

Python Mutable Function Arguments

If one of these parameters is modified in the body of the function to point to another value, the change will not be modified outside the function. On the other hand, if the content of any variable parameter is modified, the change will be modified outside the function.

No alt text provided for this image


Now that you know how Python handles data, it’s time to program and put what you learned into practice.

要查看或添加评论,请登录

MAHDi ABiD的更多文章

  • Portfolio Project: Go MY SQUAD

    Portfolio Project: Go MY SQUAD

    Description The point of this project is to find a way for Gamers to meet and find new team members from different…

  • Internet of Things (IoT)

    Internet of Things (IoT)

    What is internet of things (IoT)? The internet of things (IoT) is a network of dedicated devices -- called things --…

  • STEM: Full Stack Web Development

    STEM: Full Stack Web Development

    What is STEM? We know what you’re thinking – “Yeah but, what is STEM? What does the STEM acronym look like?” The STEM…

  • What happens when you type https://www.holbertonschool.com in your browser and press ?

    What happens when you type https://www.holbertonschool.com in your browser and press ?

    Introduction We are using the internet every day and we visit a lot of websites but surely you've got ever used the web…

  • The differences between static and dynamic libraries

    The differences between static and dynamic libraries

    In programming, a library may be a collection of pre-compiled pieces of code which will be reused during a program…

  • What happens when you type "ls -l *.c" in the shell

    What happens when you type "ls -l *.c" in the shell

    First of all, we'd like to understand what's a shell, a shell is the command interpreter between the user and the…

  • C static libraries

    C static libraries

    Why use libraries : A library is helpful because it allows programmers to skip writing some complicated but frequently…

  • What happens when you type GCC main.c

    What happens when you type GCC main.c

    When you want to execute code to create an object file, the computer needs to be communicated with in machine language,…

  • What is the difference between a hard link and a symbolic link?

    What is the difference between a hard link and a symbolic link?

    Hello there again. In this introduction to hard links and symbolic links, we will first try to learn and define what…

社区洞察

其他会员也浏览了