In Python everything is object!
source: https://www.google.com/search?biw=741&bih=392&tbm=isch&sxsrf=ACYBGNTfAZ3R9PWx4ZtNJDvAT9DsIhSqvw%3A1570592046049&sa=1&ei=LlWdXYDDAs2K5wKby6mAAQ&q=python+objects+free+images&oq=python+objects+free+images&gs_l=img.3...114.4417..4567...1.0..0.284

In Python everything is object!

Guido Van Rossum created Python according to the idea of “first-class everything” that means all in Python is an object and can be treated in the same way. This entails that every variable holds an object instance.

Let's start by breaking down some concepts!

What is id?

Id() is a built-in function in Python that returns the identity of an object. The identifier is pointing to a location in memory, which is an object.

No alt text provided for this image

When you create an integer in the range -5 and 256, you get back a reference to the already existing, because Python keeps an array of integer objects for that range.

Two macros are used for this NSMALLNEGINTS and NSMALLPOSINTS If the value ival is within range, then it calls the function get_small_int

What is type?

The type()function returns the type of the object that’s provided as its argument.

No alt text provided for this image


What are mutable objects?

In Python some objects can be changed without altering the id and can change its state or contents, after the object is created. such as: dictionary, list, byte array and sets

Let's see an example:

No alt text provided for this image

Here, you can see, the list was changed, but the id, never was altered.

What are immutable objects?

An immutable object it is the opposite to mutable objects.

There is not changeable and its state can not be modified after it is created. You cannot overwrite the values of immutable objects, but you can assign the variable again.

Some examples are: int, string, float, complex, frozen set [note: immutable version of set], bytes, tuple

No alt text provided for this image

In this example, you can see that after the addition (". Enjoy!") the string changes the id of and does not impact the alias.

Python containers liked tuples are immutable. The tuple itself cannot be modified, but objects referenced by the tuple can be modified. That means value of a tuple can't be changed after it is created. But the "value" of a tuple is in fact a sequence of names with unchangeable bindings to objects.

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

Immutability may be used to ensure that an object remains constant throughout your program. The values of mutable objects can be changed at any time and place, whether you expect it or not.

Python, takes advantage of this alias property of immutable objects to conserve space, that means that this will automatically make an alias of immutable objects that have the same value (with a few exceptions.)

You can change a single value of a mutable data type and it won’t change its memory address. However, you can’t change a single value of an immutable type. It will throw an error.

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

The way that the Python compiler handles function arguments has to do with whether the objects in the function arguments are mutable or not immutable.

If a mutable object is called by reference in a function, it can change the original variable itself. If you want to avoid changing the original variable, you need to copy it to another variable.

When immutable objects are called by reference in a function, its value cannot be changed.

When the value is called by the function, only the value of the variable is passed, not the object itself. So the variable referencing the object is not changed, but the object itself is being changed but within the function scope only. Hence the change is not reflected.

I hope this article was useful for you... See you next time!

Angie A.

IT Headhunter ??| Business Administration ??| Digital Marketing

2 年

Daniela, very interesting!??

回复

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

社区洞察

其他会员也浏览了