Monday, March 15, 2021

Points to Remember on OOPS in Python

 

  Points to Remember on OOPS in Python

 

* Procedure oriented Programming Approach is the methodology where programming is done using procedures and functions .

 

* Procedure Oriented Programming Approach is generally followed by languages like C , Pascal and Fortran

 

* Python programming can take up programming using procedure oriented approach ( like C ) or Object Oriented Programming Approach ( like Java and C++ ) depending upon requirement .

 

* An object is anything that really exists in the world and can be distinguished from other objects . It is like an entity which has a unique existence and also displays some form of attributes and behaviours.

 

* Every Object has some behaviour that is characterised by attributes and actions . Here , attributes means the manner of conductance of any action . Attributes are represented by variables and actions are performed by methods . So , an object contains variables and methods .

 

* A function which is written inside a class is called as Method .

 

* A Class is a model or Blueprint for the creation of objects . And , a class contains variables and methods .

 

* Objects are created from a Class * An object does nothing without a class , however a class can exist without any object .

 

* Encapsulation is a mechanism where the data ( variables ) and the code ( methods ) that act on the data are bound together .

 

* Class is an example for Encapsulation since it contains both data and code .

 

* Hiding the unnecessary data and code from the user is called as Abstraction .

 

* In order to hide variables or methods , one should declare the variables or methods are private members . This is done by writing two underscores before the names of the variable or the methods .

 

* Private Members can be accessed using "Name Mangling" where the class name is used with single underscore before it and two underscores after it in the form of :

 

instancename._Classname_variable

instancename._Classname_method()

 

 

* Creating new classes from existing classes so that new classes would acquire all the features of the existing classes is called as Inheritance .

 

* In Inheritance , the first class generally created is called as Base Class or Super Class . The newly created Class is called as Sub-Class or Derived Class .

 

* Polymorphism represents the ability of an object or method to assume several different forms

 

* The Programming Languages which follow all the five features of OOPS namely : classes and objects , Encapsulation , Abstraction , Inheritance and Polymorphism are called as Object Oriented Programming Languages . For example: C++ , Java and Python fall into this category of Programming Languages .

 

Classes and Objects in OOPS - a short introduction in Python

 


     Classes and Objects in OOPS

 

* A Class is a model or plan to create objects . This means , one can write a class with the attributes and actions of objects .

 

* Attributes are represented by variables and actions are performed by methods. Therefore , a class contains both variables and methods .

 

* The same variables and methods are also available within the objects because the objects are created from the class to which the variables and methods belong.

 

* These variables are also called as 'instance variables' because these instance variables can be created inside an instance like an object .

 

* One should remember the difference between a function and a method. A function which is written inside a class is called as a Method.

 

* Generally, a Method is called using one of the following two ways :

1) classname.methodname()

2) instancename.methodname()

 

 

* The general format of a class is given in the following manner:

Class Classname(object):

def _init_(self):

def method01():

def method02():

 

Friday, March 12, 2021

Concept of Abstraction in OOPS


Concept of Abstraction in OOPS

 * There may be a lot of data present within a class but the user using the system application may not need the entire data present within a class. The user requires only some part of the available data in the Class. In the following case, one can hide the unnecessary data from the user and expose only those data that is of interest to the user . This concept is called as Abstraction.

 * A good example for depicting the concept of Abstraction is that of a Car. Any Car would have some parts like Engine , Radiator , Battery , Mechanical and Electrical Equipment etc .

 * The user of a car ( driver ) should know how to drive a car and the driver may not require any knowledge of the parts of the car . For example , a driver is never bothered about how the engine is designed and the internal parts of the engine . This is the main reason why the car manufacturers hide their parts from the driver in a separate panel which is generally at the front part of the car .

 * The sole advantage for usage and putting into practice the concept of Abstraction is that every user would be able to get their own view of the data according to their own requirement and the user will not get confused with so much unnecessary data .

 * A Bank Clerk should only see the customer details like Account Number , Name and Balance Amount within an account . He should not be entrusted with access into sensitive data like the staff 's salaries , profit or loss of the bank , interest amount paid by the bank , loan amount to be recovered etc . Therefore , sensitive data should be abstracted from the Clerk's view . The Bank Manager however may require the sensitive data and thus it could be provided to the Bank Manager .

 

Introductory Concept of Abstraction in Python


 

Introductory Concept of Abstraction in Python


* In languages like Java , there are several keywords like private , public and protected in order to implement the various levels of abstraction . These keywords are called as Access Specifiers .

* In Python , such kind of Access Specifiers are not available .

* Everything written inside the class comes under the category of public – that means all the objects , methods , variables present within any class in Python can be considered to be coming under Public Category which means that everything which is written inside a class is available outside the class to other people .

* Suppose , one does not want to make a variable outside the class or to other members inside the class , then we can write the variable with two double scores before it as : _var . This is like a private variable in Python .

* In the following example , 'y' is a private variable since it is written as : __y

class MyClass:

    def _init_(self):

        self.__y = 3

* Now , it is not possible to access the variables from within the class or outside of the class as :

m = MyClass()

print(m.y)

* The preceding print() statement displays an error message as : AttributeError :'MyClass' object has no attribute 'y' .


* Even though one can not access the private variable in this way .. however , it is possible to access it in the format :

instancename.__Classname_var


* This means that we are using the Classname differently to access the private variable . This concept is known as name mangling . In name mangling , one needs to use one underscore before the classname and two underscores after the classname . Like this , using the names differently to access the private variables is called as name mangling .

 * For example , in order to display a private variable 'y' value one can write the following code :

 print(m._MyClass_y)

The same statement can be written inside the method as :

print(self._MyClass_z) . When we use single underscore before a variable as _var , then that variable or object will not be imported into other files .

Concept of Encapsulation in OOPS in Python ( in bullet points )


 

    Concept of Encapsulation in OOPS in Python

* Encapsulation is a mechanism where the data ( variables ) and the code ( methods ) that act on the data are bound together .

 * For example , if we consider a class , then we can write the variables and methods inside the class . Therefore , a class is binding them together . Hence , a class is an example for encapsulation .

 * The variables and methods of a class are called as 'members' of the class . All the members of a class are by default available outside the class . This means that all the class members are public by default . Public means available to other programs and classes .

 * Python follows Uniform Access Principle that says that in OOPS all the members of the class whether they are variables or methods should be accessible in a uniform manner .

 * Therefore , Python variables and methods are available outside alike . This means both the variables are public by default .

 * Usually in C++ and java type languages , - variables are kept private which means that they are not available outside the class and the methods are kept public which means that they are available to other programs

 * But in Python , both the variables and methods are public by default

 * Encapsulation isolates the members of a class from the members of another class . The reason is that when objects are created , each object shares different memory and hence there will not be any overwriting of data .

 * This gives an advantage to the programmer to use same names for the members of two different classes . For example , a programmer can declare and use the variables like 'id' , 'name' and 'address' in different classes like Employee , Customer or Student Class .

 

Creating Classes and Objects in Python



     Creating Classes and Objects  in Python

 

* One can create a class "Person" in which "Andy" and "Bandy" are the names of the objects within the class

 

* A Class can be created by using the keyword "class" infront of the name of the class that one is trying to make .

 

* A Class describes the attributes and actions performed by the objects of the class . Therefore , one can write the attributes ( Variables ) and actions ( Functions ) in the class as :

 ===================================================

# This is a Class with the name Person attributes means variables

  Class Person:

  name = 'Andy'

  age = 20

# actions means functions

  def talk(cls):

        print(cls.name)

        print(cls.age)


  

* One can observe from the given code that the Person Class has two variables and one function . The function that is written in the class is called a Method .

 

* If someone wants to use this Class , then one should create an Object for the class first in the given manner .

 p1 = Person()

 Here , p1 is an object of the Person Class . Object 'p1' represents memory to store the actual data . The memory needed to create object 'p1' is provided by the PVM . One may be able to observe the function / method in the class in the given manner:

 

def talk(cls):

 

Here , 'cls' represents a default parameter that indicates the class of the object . Therefore , cls.name refers to the class variable 'Andy' . One can call the "talk()" method to display the object's details as :


p1.talk()

===========================================================

Thursday, March 11, 2021

Most Important Conceptual Problems Associated with Procedure Oriented Programming Approach and why OOPS was conceptualised

 


Problems in Procedure Oriented Approach


* In Procedure Oriented Programming Approach, the Programmer concentrates on a specific task and writes a set of functions to achieve the task.

 

* When there is another task to be added to the software, one should write another set of functions

 

* This means that the programmers concentration should be only on achieving the tasks.

 

* The Programmer perceives the entire system as fragments of several tasks. The Programmer perceives the entire system as fragments of several tasks.

 

* Whenever the Programmer wants to perform a new task , the Programmer would write a new set of functions . Therefore, there is no reusability of already existing code.

 

* A new task every time requires developing the code from the scratch . This is considered as a Waste of Programmer's time and effort .

 

* In Procedural Oriented Programming Approach, every task and sub- task is represented as a function and one function may depend on another function for accomplishment of a task or a set of related tasks which may be parallelly executed or serially executed.

 

* Therefore, when an error occurs over the software, the software needs examination of all the functions. Therefore, debugging or removing the errors is one of the most difficult operations to be performed. And as such, any updations to the software is very difficult to be achieved.

 

* When the software is developed, naturally the code size would also increase as the size of the code base increases .

 

* It has been observed in most of the software developed following the Procedure Oriented Approach that when the code size exceeds 10,000 lines and when it reaches 100,000 lines of code , then suddenly at a particular point , the programmers start losing control over the code .

 

* This means that , the programmers could not understand the exact behaviour of the code and could neither debug it nor extend it . This posed many problems , especially when the software was constructed to handle bigger and more complex systems .

 

* For example , in order to create software to send satellites into the sky and