Monday, March 15, 2021

Self Variable in _init_ constructor

 


Self Variable in _init_ constructor


* 'Self' is a default variable that contains the memory address of the instance of the current class which is under current usage .

 

* So , one can use 'self' instance variable to refer to all the instance variables and Instance Methods .

 

* When an instance of the class is created , the

instance name contains the memory location of the instance . The memory location is internally passed to the 'self' . For example , one can create an instance of the Student Class in the given manner :

 

S1 = Student()

 

* Here , 's1' contains the memory address of the instance . This memory address is internally and by default passed to the 'self' variable .

 

 

* Since the 'self' knows the memory address of the instances , it can refer to all the members of the instance .

 

* One can use 'self' in the following ways :

1) The 'self' variable is used as a first parameter within the constructor as :

 

def _init_(self):

In this case , 'self' can be used to refer to the instance variables inside the constructor .

 

2) 'self' can be used as a first parameter in the instance method as :

 

def talk(self):

 

* Here , talk() is an instance method as it acts on the instance variables present within the Class and defined under the _init_(self): method .

 

* If the method wants to act on the instance variables , then the method should know the memory location of the instance variables . That memory location is by default available to the talk() method through the 'self' method .

Concept of Polymorphism in OOPS

 


Polymorphism


* The word "Polymorphism" comes from two Greek words "Poly" meaning "many" and "morphos" meaning "forms".

 

* Therefore , Polymorphism represents the ability to assume several different forms

 

* In Programming , if an object or a method is

exhibiting different form of behaviours , then the object is said to having polymorphic nature .

 

* Polymorphism provides flexibility in writing programs in such a way that the programmer uses same method call to perform different operations depending upon the requirement .

 

* Example Code for Polymorphism in Python :

 

- When a function can perform different tasks , then one can say that the function is exhibiting polymorphism behaviour .

 

- A simple example to write a function :

 

  def add(a,b):

       print(a+b)


* Since in Python , the variables are not declared explicitly , we are passing two variables 'a' and 'b' to the add() function where the variables are added to each other .

* CASE 01 - While calling the function , if we pass two integers like 5 and 15 to the function , then the function displays 15 as output .

 

* CASE 02 - If we pass two strings , then the same function concatenates or joins those strings . This means that the same function is adding two integers or concatenating two strings .

 

* Since the Polymorphism function is performing two different tasks , it is said to exhibit polymorphism behaviour .

 

* One can consider the following example for the case of Polymorphism :

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

# Function that exhibits Polymorphism

def add(a,b):

    print(a+b)

 

# calling add() function and passing two integers

add(10,20)

 

# calling add() function and passing two strings

add("Core","Python"

 

 

* The Programming Languages which follow all the five features of OOPS – ( Classes with objects Abstraction , Encapsulation , Inheritance , Polymorphism) are called as object oriented programming languages .

 

* These type of behaviours are exhibited by C++ , Java and Python type of languages

Types of Variables in Python ( related to Classes and Objects in OOPS )

 


Types of Variables in Python

 

* The variables which are written inside a class are of primarily two types :

1) Instance Variables

2) Class Variables / Static Variables

 

* Instance variables are the variables whose separate copy is created in every instance ( or object )

 

* For example , if 'x' is an instance variable and if we create 2 instance variables then there will be 2 copies of 'x' in the instances .

 

* When we modify the copy of 'x' in any instance, then it will not modify the other two copies .

 

==========================================
Python Program to understand Instance Variables
==========================================

 

# instance variable example

Class Sample:

    # this is a constructor

    def __init__(self):

    self.x = 10

 

    # this is an instance method

   def modify(self):

   self.x += 1

 

 

# Create 2 instances

    s1 = Sample()

    s2 = Sample()

    print(' x in s1 = ', s1.x)

    print(' x in s2 = ', s2.x)

 

# modify x in s1

     s1.modify()

    print(' x in s1 = ' , s1.x)

    print(' x in s2 = ' , s2.x)

 

# modify x in s1

    s1.modify()

    print(' x in s1 = ', s1.x)

    print(' x in s2 = ', s2.x)

 

Output

x in s1 = 10

x in s2 = 10

x in s1 = 11

x in s2 = 10

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

 

* Instance Variables are defined and initialised using a constructor with 'self' parameter . In the above program it can be realised in the line just after the usage of the docstring over where the code for declaration and definition of the Instance Variables is mentioned and in the succeeding line the __init__(self) is defined .

 

* In order to access the instance variables one needs Instance methods with 'self' as the first parameter which can be observed from the above code as well

 

* It is possible that the instance methods may have other parameters in addition to the 'self' parameter .

 

* In order to access the instance variables , one can use a self.variable in a program .

 

* It is also possible to access the instance variables from outside the class as : instancename.variable e.g. s1.x .

 

* Unlike instance variables , class variables are the variables whose single copy is available to all the instances of the class .

 

Example of Inheritance Concept ( in OOPS ) in Python

 

 

Example of Inheritance in Python

 

* In Python lets assume that , one can take a class A with two variables 'a' and 'b' and a method like method01() in the given manner .

 

* Since all the methods are needed by another class B , one can extend class B from class A .

 

* If we want some additional members in object B , for example a variable 'c' and a method in the form of method02() . These methods are written in object B .

 

* From this one would remember that class B can use all the members of both A and B .

 

* This means that all the variables 'a' , 'b' , 'c' and the methods method01() and method02() are available to the class B . This means that all the members of class A are inherited by class B .

 

<< FIG >>

 

 

* By creating an object of B , one can access all the members of both the classes A and B that is when an object of class B is created which is a form of derived class object , from the base class , one can create another object .

 

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 .