Python3: Everything is an object!

Python3: Everything is an object!

This is the Key phrase: Everything in Python is an object.

As in real life, everything in Python is made up of objects and these objects have characteristics and properties. For example, a house, which is the object of real life, has properties such as dimension, color, location, etc. Another important concept that every newcomer to Python should quickly learn is that all objects in Python can be either mutable or immutable. These concepts will be deepened later.

In this article, I will discuss concepts such as id and type, mutable and immutable objects, why does it matter and how differently does Python treat mutable and immutable objects, how arguments are passed to functions and what does that imply for mutable and immutable objects, among other concepts related to Object-oriented programming (OOP).

No hay texto alternativo para esta imagen

ID and TYPE

Id() Function:

Python id() is a function that returns the “identity” of the object. The identity of an object is an integer, which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. In other words, Python cache the id() value of commonly used data types, such as string, integer, tuples, etc. So you might find that multiple variables refer to the same object and have the same id() value if their values are the same.

Example, Input:

# integers
a = 10
b = 10
c = 11
d = 12

print(id(a))
print(id(b))
print(id(c))
print(id(d))

Output:

140464292410944
140464292410944
140464292410976
140464292411008

type() Function:

type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes. In other words, Ththeuilt-in function type() returns the type of an object. Let's look a atn example:

Syntax :

>>>type(object) #Object
>>>type(name, bases, dict) #Parameters

Parameters :

name : name of class, which later corresponds to the __name__ attribute of the class.

bases : tuple of classes from which the current class derives. Later corresponds to the __bases__ attribute.

dict : a dictionary that holds the namespaces for the class. Later corresponds to the __dict__ attribute.

>>> Returntype :
Returns a new type class or essentially a metaclass.

Input code:

# Python3 simple code to explain 
# the type() function

print(type([]) is list) 


print(type([]) is not list) 


print(type(()) is tuple) 


print(type({}) is dict) 


print(type({}) is not list) 

Output:

True
False
True
True
True


MUTABLE and IMMUTABLE OBJECTS

Simply put, a mutable object can be changed after it is created, and an immutable object can’t. So, which objects are mutable and which are immutable?

The answer can be seen in the following set of elements: Objects or Classes of built-in types like (int, float, bool, str, tuple, Unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable.

No hay texto alternativo para esta imagen

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

Mutable and Immutable Objects are used in specific cases related to the concepts of Type and Identity that we saw earlier.

No hay texto alternativo para esta imagen

A practical example to find out the mutability:

#create two variables
x = 10
y = a

We are creating an object of type int. identifiers x and y points to the same object. To check that both x and y are pointing to the same object, check the id of x and y:

Input code:

x = 10
y = x
print("id of x =",id(x))
print("id of y =",id(y))

Output:

id of x = 1579869088
id of y = 1579869088

A practical example to find out the immutability:

Immutable objects do not allow modification after creation:

Input code:

# Python code to test that 
# tuples are immutable 
	
tuple1 = (0, 1, 2, 3) 
tuple1[0] = 4
print(tuple1)

Output: Error

Traceback (most recent call last):
  File "C:/Python36/Test.py", line 5, in 
    tuple1[0] = 4
Type
Error: 'tuple' object does not support item assignment


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

  • Python handles mutable and immutable objects differently.
  • Immutable are sicker to access than mutable objects.
  • Mutable objects are great to use when you need to change the size of the object, example list, dict et,c.. Immutables are used when you need to ensure that the object you made will always stay the same.
  • Immutable objects are fundamentally expensive to “change”, because doing so involves creating a copy. Changing mutable objects is cheap.


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

David Castellanos的更多文章

社区洞察

其他会员也浏览了