English 中文(简体)
Introduction
  • 时间:2024-12-22

Object Oriented Python - Introduction


Previous Page Next Page  

Programming languages are emerging constantly, and so are different methodologies.Object-oriented programming is one such methodology that has become quite popular over past few years.

This chapter talks about the features of Python programming language that makes it an object-oriented programming language.

Language Programming Classification Scheme

Python can be characterized under object-oriented programming methodologies. The following image shows the characteristics of various programming languages. Observe the features of Python that makes it object-oriented.

Langauage Classes Categories Langauages
Programming Paradigm Procedural C, C++, C#, Objective-C, java, Go
Scripting CoffeeScript, JavaScript, Python, Perl, Php, Ruby
Functional Clojure, Eralang, Haskell, Scala
Compilation Class Static C, C++, C#, Objective-C, java, Go, Haskell, Scala
Dynamic CoffeeScript, JavaScript, Python, Perl, Php, Ruby, Clojure, Erlang
Type Class Strong C#, java, Go, Python, Ruby, Clojure, Erlang, Haskell, Scala
Weak C, C++, C#, Objective-C, CoffeeScript, JavaScript, Perl, Php
Memory Class Managed Others
Unmanaged C, C++, C#, Objective-C

What is Object Oriented Programming?

Object Oriented means directed towards objects. In other words, it means functionally directed towards modelpng objects. This is one of the many techniques used for modelpng complex systems by describing a collection of interacting objects via their data and behavior.

Python, an Object Oriented programming (OOP), is a way of programming that focuses on using objects and classes to design and build apppcations.. Major pillars of Object Oriented Programming (OOP) are Inheritance, Polymorphism, Abstraction, ad Encapsulation.

Object Oriented Analysis(OOA) is the process of examining a problem, system or task and identifying the objects and interactions between them.

Why to Choose Object Oriented Programming?

Python was designed with an object-oriented approach. OOP offers the following advantages −

    Provides a clear program structure, which makes it easy to map real world problems and their solutions.

    Faciptates easy maintenance and modification of existing code.

    Enhances program modularity because each object exists independently and new features can be added easily without disturbing the existing ones.

    Presents a good framework for code pbraries where suppped components can be easily adapted and modified by the programmer.

    Imparts code reusabipty

Procedural vs. Object Oriented Programming

Procedural based programming is derived from structural programming based on the concepts of functions/procedure/routines. It is easy to access and change the data in procedural oriented programming. On the other hand, Object Oriented Programming (OOP) allows decomposition of a problem into a number of units called objects and then build the data and functions around these objects. It emphasis more on the data than procedure or functions. Also in OOP, data is hidden and cannot be accessed by external procedure.

The table in the following image shows the major differences between POP and OOP approach.

Difference between Procedural Oriented Programming(POP)vs. Object Oriented Programming(OOP).

Procedural Oriented Programming ObjectOriented Programming
Based On In Pop,entire focus is on data and functions Oops is based on a real world scenario.Whole program is spanided into small parts called object
Reusabipty Limited Code reuse Code reuse
Approach Top down Approach Object focused Design
Access specifiers Not any Pubpc, private and Protected
Data movement Data can move freely from functions to function in the system In Oops, data can move and communicate with each other through member functions
Data Access In pop, most functions uses global data for sharing that can be accessed freely from function to function in the system In Oops,data cannot move freely from method to method,it can be kept in pubpc or private so we can control the access of data
Data Hiding In pop, so specific way to hide data, so pttle bit less secure It provides data hiding, so much more secure
Overloading Not possible Functions and Operator Overloading
Example-Languages C, VB, Fortran, Pascal C++, Python, Java, C#
Abstraction Uses abstraction at procedure level Uses abstraction at class and object Level

Principles of Object Oriented Programming

Object Oriented Programming (OOP) is based on the concept of objects rather than actions, and data rather than logic. In order for a programming language to be object-oriented, it should have a mechanism to enable working with classes and objects as well as the implementation and usage of the fundamental object-oriented principles and concepts namely inheritance, abstraction, encapsulation and polymorphism.

Oops

Let us understand each of the pillars of object-oriented programming in brief −

Encapsulation

This property hides unnecessary details and makes it easier to manage the program structure. Each object’s implementation and state are hidden behind well-defined boundaries and that provides a clean and simple interface for working with them. One way to accomppsh this is by making the data private.

Inheritance

Inheritance, also called generapzation, allows us to capture a hierarchal relationship between classes and objects. For instance, a ‘fruit’ is a generapzation of ‘orange’. Inheritance is very useful from a code reuse perspective.

Abstraction

This property allows us to hide the details and expose only the essential features of a concept or object. For example, a person driving a scooter knows that on pressing a horn, sound is emitted, but he has no idea about how the sound is actually generated on pressing the horn.

Polymorphism

Poly-morphism means many forms. That is, a thing or action is present in different forms or ways. One good example of polymorphism is constructor overloading in classes.

Object-Oriented Python

The heart of Python programming is object and OOP, however you need not restrict yourself to use the OOP by organizing your code into classes. OOP adds to the whole design philosophy of Python and encourages a clean and pragmatic way to programming. OOP also enables in writing bigger and complex programs.

Modules vs. Classes and Objects

Modules are pke “Dictionaries”

When working on Modules, note the following points −

    A Python module is a package to encapsulate reusable code.

    Modules reside in a folder with a __init__.py file on it.

    Modules contain functions and classes.

    Modules are imported using the import keyword.

Recall that a dictionary is a key-value pair. That means if you have a dictionary with a key EmployeID and you want to retrieve it, then you will have to use the following pnes of code −

employee = {“EmployeID”: “Employee Unique Identity!”}
print (employee [‘EmployeID])

You will have to work on modules with the following process −

    A module is a Python file with some functions or variables in it.

    Import the file you need.

    Now, you can access the functions or variables in that module with the ‘.’ (dot) Operator.

Consider a module named employee.py with a function in it called employee. The code of the function is given below −

# this goes in employee.py
def EmployeID():
   print (“Employee Unique Identity!”)

Now import the module and then access the function EmployeID

import employee
employee. EmployeID()

You can insert a variable in it named Age, as shown −

def EmployeID():
   print (“Employee Unique Identity!”)
# just a variable
Age = “Employee age is **”

Now, access that variable in the following way −

import employee
employee.EmployeID()
print(employee.Age)

Now, let’s compare this to dictionary −

Employee[‘EmployeID’] # get EmployeID from employee
Employee.employeID() # get employeID from the module
Employee.Age # get access to variable

Notice that there is common pattern in Python −

    Take a key = value style container

    Get something out of it by the key’s name

When comparing module with a dictionary, both are similar, except with the following −

    In the case of the dictionary, the key is a string and the syntax is [key].

    In the case of the module, the key is an identifier, and the syntax is .key.

Classes are pke Modules

Module is a speciapzed dictionary that can store Python code so you can get to it with the ‘.’ Operator. A class is a way to take a grouping of functions and data and place them inside a container so you can access them with the ‘.‘operator.

If you have to create a class similar to the employee module, you can do it using the following code −

class employee(object):
   def __init__(self):
      self. Age = “Employee Age is ##”
   def EmployeID(self):
      print (“This is just employee unique identity”)

Note − Classes are preferred over modules because you can reuse them as they are and without much interference. While with modules, you have only one with the entire program.

Objects are pke Mini-imports

A class is pke a mini-module and you can import in a similar way as you do for classes, using the concept called instantiate. Note that when you instantiate a class, you get an object.

You can instantiate an object, similar to calpng a class pke a function, as shown −

this_obj = employee() # Instantiatethis_obj.EmployeID() # get EmployeId from the class
print(this_obj.Age) # get variable Age

You can do this in any of the following three ways −

# dictionary style
Employee[‘EmployeID’]
# module style
Employee.EmployeID()
Print(employee.Age)
# Class style
this_obj = employee()
this_obj.employeID()
Print(this_obj.Age)
Advertisements