Translate in Your language

Saturday, 28 March 2020

Learning Python: part-3 - Python Objects


Since beginning, we are referring to Object oriented language paradigm and its close relation to Python. So let's go ahead and understand, what is an Object in python language or any other OOP based programming language.


  • According to official Python documentation: 
    • Objects are Python's abstraction for data. All data in a Python program is represented by objects or by relations between objects." 
The literal meaning of above statement sounds like everything in python is an object. Well, that may not clear the concept what exactly it means.  Let's try to understand it with some real life example.

"First of all object can be anything that you wish to declare as per your program requirement. "

Let's imagine you are sitting in front of your desktop computer at your office. Let's analyse before you start working. What you see?

Don't shout COMPUTER :)                                

Reason? 

Well, there are multiple devices like Monitor, Keyboard, Mouse, Cabinet (Don't go inside cabinet looking for M/board, RAM and processor assume cabinet as processing unit), some cables at least this much you have bare minimum, let's skip add ons. 

Now, Can we call them objects in real life? 

Of course, we can. And they are used as per usage requirement and they do coordinate with each other. 

Now, if you analyse further, there will be some inventory code labels generally to every object that we talked.

So this inventory label, what purpose does it serve?   

Hmm, that's the ID assigned to it for identification purposes in inventory.

Now in this whole scenario, I am talking about generic identifications I have not talked about specific vendor or model or anything like that. Can I use above objects in Python.

100% you can.

Let us see that with an example


So in above program, I used Keyboard, Mouse,  Cabinet, Montior as objects and assigned some value to it. Then just printed the output along with their IDs (Memory location where object is created and stored).


Basically in programming, Object can be any entity depending upon business requirement. So in Python, when we define variable (This is what we used), class or functions in program, everything is object.

Let's take another example of any grocery store. It might have objects like "shopping cart", "customer", and "products”. GO ahead and write a program using those terms.

I hope this clears the concept of Object in programming language. And this is the beauty of OOP, where we use terms of real life entities in programming and relate the program to business model.

Now some key point.

  • Every object in Python has an identity, a type, and a value.
    • In our example, object type will be str (means string), value that we assigned like Aurora, and others, id is what we printed.
    • I will talk about types later in this series so hold on.
  • Once created, the ID of an object never changes. It’s an unique identifier for it. It is used by Python to retrieve the object when we want to use it like when I said print(object).
  • The type never changes. The type represents possible values and possible operations on object.
  • The value can may change or not.
    • If value can be changed, the object is mutable,
    • If value cannot be changed, the object is immutable.

The two main features any object has are properties and methods.

  • Properties are nothing but characteristics of an object
    • like in our example, for Monitor, color, size, resolution are examples of object properties
  • Methods means, things we can perform with/on object.
    • Example methods for our monitor object, power on, power off, change resolution, set frequency like that
That's it folks. I hope its helpful. Cheers!!!

No comments:

Post a comment

Followers

Popular Posts This Week