Skip to main content

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 :). "I will use it later for explaining classes. 

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!!!


Popular posts from this blog

Learning Python Part-6: Python Comments

When it comes to writing a python program, based on the requirements, programs can simple programs of limited number of  lines or it can be really large program which might even span multiple python modules. Now the point here is that, even if we know how to write the program and even we follow correct concepts and syntaxes while writing program, we cannot keep it in our mind forever. We cannot remember each and everything about the program like variables, classes, concepts, functions and so on after some period of time. If the program written by us at some point in time back, once we open it after a period of time, it should not be a situation that we are blank, or guessing why particular code, function and so on. Even it may happen that you are working in a team and program code writing tasks might be shared. So when you put all pieces together written by multiple programmers in your team, you should be able to understand the code. This is where we bring in the concepts of

Learning Python Part-4: Python Keywords

Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive. There are 33 keywords in Python 3.7.  All the keywords (except True, False and None) are in lowercase. Below is the list of keywords in Python 3.7 I will not be using just syntax to explain these keywords, rather I will use some simple yet enough code examples considering new plus experienced people. For those who are new to python, if you find it difficult  to understand the complete code, just try to see the context and usage of keyword.  False :  This keyword is used to represent a boolean false. If a statement is false, “False” is printed. False in python equals to 0. True: This keyword is used to represent a boolean true. If a statement is true, “True” is printed. True in python equals to 1. None:

Learning Python - Part-5: Python Interfaces

When it comes to working with python, we can always use interactive mode by launching python  or Python3 command as shown in below screenshot.  Image: 1 Once launched we can run all sorts of python operations, however, this approach is not used for development work.  When it comes to writing programs, we generally prefer editors. In fact we can use basic editors like notepad(Windows)/vi editors(Linux). In these editors, once we are done with writing programs, we need to make sure files are stored as .py extension ( Image: 2 ). We simply run python command to execute the programs as shown in below screenshot ( Image: 3 ). Image: 2 Image: 3 Though using normal editors is fine, but when it comes to writing simple, large or complex programs, these basic editors do not offer help or suggestions or correction while writing code. That is where the need of tools that help during programming arises. Below are some of the code editors and IDEs that are widely used.