Everything is an object

Everything is an object

Python is an object oriented programming language. Almost everything in python is an object, with its methods, properties, instances, attributes, etc. We can say that a class is an object constructor, or a "blueprint" for creating objects.

The id

The id of an object is an unique identification (which is an integer) for an object. All objects in python has its own unique id. the id is assigned to the object when it is created. In Python we have the id() method that returns the identifier of the object.

The type

A type is the specific category of the objects (for example an object can be type: int, list, tuple, etc). In Python we have the type() method that returns the type of the object, if only one object parameter is passed or a new type, if 3 parameters passed.

Mutable objects

Mutable is when something is changeable or has the ability to change. In Python, "mutable" is the ability of objects to change their values. These are often the objects that store a collection of data. Examples: list, dict, set.

Immutable objects

Immutable objects in Python can be defined as objects that do not change their values and attributes over time. These objects become permanent once created and initialized, and they form a critical part of data structures used in Python. Examples: int, float, bool, string, unicode, tuple.

Referencing and assignment

A Python program accesses data values through references. A reference is a name that refers to specific location in memory of a value (object). References take the form of variables, attributes, and items. In Python, a variable or other reference has no intrinsic type.

When programming. it is useful to be able to store information in variables, this is called assignment. The assignment operator, denoted by the "=" symbol, is the operator that is used to assign values to variables in Python. the line a=1 takes the known value, 1, and assigns that value to the variable with name "x". After executing this line, this number will be stored into this variable.

How works in memory?

No alt text provided for this image
No alt text provided for this image

Conclusion

Mutable and immutable objects are handled differently in Python. Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy. Whereas mutable objects are easy to change. Mutable objects can be used for example when you need to change the size or content of the object.

How we pass arguments to function

We know the difference between mutable an immutable, so we can take a look on how these are treated when they are switched to functions. The efficiency of memory could be affected when the wrong objects are used.

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

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

社区洞察

其他会员也浏览了