Is everything an object in python ?

Is everything an object in python ?

Unlike the other language, Python is an OOP(object oriented programming) language and that mean it can organizes software design around data, or objects, rather than functions and logic.

And this lead us to the question WHAT IS AN OBJECT?

Actually the oop languages define an object in different ways but they agreed in some points and that's object are instances of a class created with specifically defined data and can correspond to real-world objects or an abstract entity.

Difference between object and classes:

Object is an instance of a class. Class is a blueprint or template from which objects are created. Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc. Class is a group of similar objects.

What is an id?

It's a built-in function that's return a unique integer number or every unique value it is used with, that's integer is the variable identifier that corresponds to the memory address of the object in the CPython implementation

What is type?

It's a built-in function that's return the type of variable used to store the data elements in a program.

Mutable vs Immutable objects:

No alt text provided for this image

Every variable in python holds an instance of an object. There are two types of objects in python: Mutable and Immutable objects.

Mutable: Object that can be changed (list, dictionary, set and user-defined classes ). When you create aliases of variables, the two refer to the same object and have the same id

No alt text provided for this image

On the left, we have a list at some memory location. On the right, we add another item to our list and the object at the same memory location is directly modified.

Immutable: Object that cannot be changed ( int, float, boolean, string, tuple and user-defined classes ) unlike the mutable type.

No alt text provided for this image

The variable named a is bound to an object with value 1 in one memory location. When the variable named a is bound to a different object with value 2, the original object with value 1 is still in memory, but you can’t access it anymore through a variable.

Special case:

Tuple and Frozen Set : tuples are immutable lists, frozensets are immutable sets.

tuples are indeed an ordered collection of objects, but they can contain duplicates and unhashable objects, and have slice functionality

frozensets aren't indexed, but you have the functionality of sets - O(1) element lookups, and functionality such as unions and intersections. They also can't contain duplicates, like their mutable counterparts.

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

Python works differently on mutable and immutable objects,?the Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy y the mutable objects are easy to change.

If the size or content of the object needs to be changed, it is recommended to use mutable objects.

Exception: Immutable objects have an exception, we know that a tuple in Python is Immutable, but the tuple contains a sequence of names with immutable links to objects that can be mutable.

tuple1 = ("Mahdi", [1, 2, 3])         

The tuple1 is not mutable, but it contains a list in its element [1], and the lists if it is mutable and its content can change. As a rule of thumb, Generally, Primitive-like types are probably immutable and Customized Container-like types are mostly mutable.

What is the difference between assignment and referencing?

When you call a function in Python and give it some arguments... Are they passed by value? No! By reference? No! They're passed by assignment ??.

Assignment:

>>> a = ['test']
>>> b = ['test']
>>> a is b
False        

Reference:

>>> c = ['test']
>>> d = c
>>> c is d
True        

The first example creates 2 unique list objects, which are not the same. a is b returns false because a and b are pointing to distinct objects:

          +------+
a ------> | list |
          +------+

          +------+
b ------> | list |
          +------+        

The second example creates a single list object, and points both c and d to that objects, hence c is d return true:

          +------+
c ------> | list | <------ d
          +------+        

So is and == are very different; while the former compares object identity, the latter compares object values. Indeed, == tests in your snippets would return true.

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

And know we kn the difference between mutable and immutable objects and how they are treated.

Memory efficiency is highly affected when the proper objects are used.

For example if a mutable object is called by reference in a function, it can change the original variable itself. Hence to avoid this, the original variable needs to be copied to another variable. Immutable objects can be called by reference because its value cannot be changed anyway.

>>> def increment(n):
...    n += 1
...
>>> a = 3
>>> increment(a)
>>> a
3>>> def increment(l):
...     l += [4]
...
>>> l = [1, 2, 3]
>>> increment(l)
>>> l
[1, 2, 3, 4]        

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

Mahdi Bani的更多文章

社区洞察

其他会员也浏览了