TowardsMachineLearning

Introduction to OOP ( Object Oriented Programming )

Introduction:-

Python is an “object-oriented programming language ” that means programs are organized around data, or objects, rather than functions and logic.  In object-oriented programming one uses classes to keep related things together and this is done using the keyword “class,” which is a grouping of object-oriented constructs.

If you do not have any previous experience with object-oriented (OO) programming, you may want to go through an introductory course on it or at least a tutorial of some sort so that you have a grasp of the basic concepts.

However, here is small introduction of Object-Oriented Programming (OOP) to bring you at speed . This post aims to give a short, basic introduction to the concept of class , objects ,methods etc. using Python as the language of choice. It assumes knowledge of very basic Python syntax and functions. 

What is Object Oriented Programming?

Python has been an object-oriented language since it existed. Everything in Python is an object.

The easiest way to think of object-oriented programming (OOP) is visually. Don’t focus on the code or even the core concepts for a second. One of the classic examples is to take any object you experience in the real world and break it down. So let’s start with a car.

A car can be an object, so I create a car class. Every time I make (instantiate) a new car variable I call it something else – polo, mini, beetle, ford, honda, etc…

Let’s take CarDriver as an example to take understand it better in real world-

A CarDriver can be an object, so I create a “CarDriver” class. Every time I make (instantiate) a new CarDriver variable I call it something else – John, Paul, George  etc…

Think of whenever you’d to hire a CarDriver , Every CarDriver has a different features (class variables) : every CarDriver has a name , EmployeID . You can call different methods on this like “openDoor” , “drive” , pickup , drop etc.

Everything Is An Object…

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects.

So, everything in Python is an object. Lists are objects. An integer value 36 is an object. Modules are objects. Functions are objects. Python bytecode is also kept in an object. All of these have types and unique IDs:

>>> def func(): 
         pass

>>> type(func), id(func)
 (function, 2041193457320) 

>>> type(func.__code__), id(func.__code__)
 (code, 2041193261216) 

Every object in Python has a type. Its type can be discovered by calling the “type” builtin function . The type is an object too, so it has a type of its own, which is called type. This last fact may not be very exciting or useful when you’re just writing Python code, but it’s hugely important if you want to understand the internals of CPython:

>>> type(36)
 <'int'>
>>> type(type(36))
 <'type'>
 >>> type(type(type(36)))
  <'type'> 

Another popular buzzword that you’ll hear in object oriented programming language is “instance”.

Instance” is synonymous to “object”. Think of it this way: objects are instances of types. So, “36″ is an instance of the type int” is equivalent to “36″ is an int object”. I usually use “instance” and “object” interchangeably. In some cases when I want to specifically refer to objects as artifacts of the CPython implementation, I will try to use “instance” to refer to actual instances of classes. Another place where the term “instance” is explicitly used by Python is in built-ins like isinstance and the special __instancecheck__ attribute

Kindly note that every object has an identity, a type and a value !!

Article Credit:-

Name:  Praveen Kumar Anwla
6+ year of work experience

Leave a Comment