EVERYTHING IS AN OBJECT IN PYTHON

EVERYTHING IS AN OBJECT IN PYTHON

INTRODUCTION

In this article, I will share some little things that I have learned so far in Python as part of my software engineering journey at African Leadership University.

I am going to talk about Python class and objects. Under that, I will speak about Id and type. I will also talk about mutable and immutable objects in Python. Lastly, I will talk about how arguments are passed to python objects.

Python is a high-level programming language. Python is known as an object-oriented programming language. Almost everything in Python is an object. The object can have properties and methods. This actually, we can have a representation of nearly everything we want in Python.

In?object-oriented?programming languages like Python, an object is an entity that contains data along with associated metadata or functionality; the data contained in an object is known as the object's data attributes.

These attributes are simply variables that reference the data. The procedures or series of activities conducted in a particular order or manner that an object performs are known as the methods. An object's methods are functions that perform operations on the object's data attributes. (Dev et al., 2022)

A Class is like an object constructor or a "blueprint" for creating objects. With a class, we can make any object we want in Python.

I.D.

All objects in Python have their unique id.

The id is assigned to the object when it is created.

The id is the object's memory address and will be different each time you run the program. (except for some objects with a constant unique id, like integers from -5 to 256).("Python id() Function", 2022)

To get the id of any object in Python, use the?id()?function as you pass in the name of the thing. This will returns a unique id for the specified object. Below is a code example showing how that works.

No alt text provided for this image

After passing the variable a to the id() function, it returned to us the id of the variable as seen in the code above.

Object type

In programming, the data type is an important concept.

Variables can store data of different types, and data of various types can do different things.

Python has the following data types built in by default, in these categories:

Str, int,?float,?complex, list,?tuple,?range, dict, set,?frozenset, bool, bytes,?bytearray,?memoryview, and NoneType. When you ask to get object type, you will get feedback that your object is in one of the above classes, i.e., string or integer ("Python Data Types", 2022).

To check the type of an object in Python, use the built-in function type() as you pass in the name of the object in it. Below is a code example elaboration that.

No alt text provided for this image

In the above code, we are able to check the types of the two variables, as seen in the code the variables are of int and str class types.

Mutable and Immutable objects

The fundamental distinction that Python makes on data is if the object is mutable or immutable, that is, if the value of an object changes or not. Considering the earlier analogy, if the data value contained in the box (i.e., object) can be changed, the object is called?mutable; however, if the value cannot change, the object is called?immutable. by changing means, you can change its value from time to time. Those that are immutable, their value is not changeable once created (2022).

Mutable-This are of type?list,?dict,?and set. Custom classes are generally mutable. Meaning their content can be changed.

No alt text provided for this image

In the above code, we have changed the last value in the list from 4 to 5. when we print the new list after replacing the value, you get the list with the replaced value.

Immutable objects -There are in-built types like?int, float, bool, string, Unicode, and tuple. Simply put, an immutable object can't be changed after it is created. Below is a code example explaining immutable objects.

No alt text provided for this image

When we tried changing the tuple's last item, we got an error because a tuple is an immutable object. Its values can't be changed once it's created.

Eric NDAYISABA

Certified Digital Marketing Associate|Project Management| Communication Enthusiast |Photographer

2 年

Keep doing #hardthings beoth'angu Almarat Arnu ??????

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

社区洞察

其他会员也浏览了