Friday, March 12, 2021

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

Introduction to OOPS using Python - ( important points to remember )

 


Introduction to OOPS using Python

 

* OOPS is one of the most revolutionary concepts in Computer Science , and its full form is called as Object Oriented Programming System ( OOPS ) which is based on languages like Smalltalk , Simula-67 , C++ , Java , Python etc .

 

* The languages like C , Pascal , Fortran etc are called as Procedure Oriented Programming which perform a given set of tasks .

 

* While developing software , the main task is divided into several sub tasks and each of the sub task can be represented as a procedure or a function .

 

* The main task is therefore composed of several procedures and functions and the approach for this is called as Procedure Oriented Approach .

 

* Procedure Oriented Approach for OOPS based programs . Languages like C++ , Java and Python use classes and Objects in their programs and are called as + , Java and Python use classes and Objects in their programs and are called as Object Oriented Programming Languages .

 

* A Class is a module which itself contains data and methods (functions) to achieve the task.

 

* The main task is divided into several sub tasks and they are represented as classes. Each class can perform several inter-related tasks for which several methods are written in a class . This approach is called as Object Oriented Approach .

 

* Programmers follow a Procedure Oriented Approach for looking into problems that associate with problems related to Procedure Oriented Programming .

 

Important Points to Remember : Dictionary Objects in Python ( syntax , usage , methods , behaviour etc )

 

  Points to Remember: Dictionary Objects in Python




 

1) A dictionary represents a group of elements arranged in the form of key-value pairs. In the dictionary object , the first element is considered as the 'key' and the immediate next element is considered as its associated 'value' .

 

2) The Key and Value Pairs should be written inside a dictionary by separating them with the help of a colon (:) operator. Each pair should be separated with the help of a comma sign. All the key-value pairs of the dictionary should be written inside curly braces { }

 

3) Indexing and Slicing are not useful to access the elements of a dictionary

 

4) While inserting a new element or modifying the existing element, it is preferable to use the given format:

 

dict(key) = Value

 

5) The keys of a dictionary should be unique and must be Immutable which means that once the data for the Keys is assigned, one cannot change the data type of the elements inside the dictionary.

 

6) The keys of a dictionary should be unique and belong to immutable datatype . The value associated with the key should be unique and should be immutable in nature.

 

7) The get(k,v) method returns the value upon taking the key 'k' . If the key is not found in the dictionary , then it will return a default value 'v'

 

8) The update({k:v}) method stores the key 'k' and its value 'v' pair into an existing dictionary

 

9) The dict() method converts a list or tuple or a zip object into a dictionary

 

10) The zip() method is useful to convert the sequences like lists into a zip class object

 

11) An ordered dictionary is a dictionary but it will keep the order of the elements

 

12) Ordered Dictionaries are created using the OrderedDict() method of collections module .

 

A descriptive article on - What are Ordered Dictionary Objects in Python and how to create them ( code over Jupyter Notebook IDE )

 



Ordered Dictionaries in Python


 

* The elements of a dictionary are not ordered . It means that the elements are not stored in the same order as they were entered into a dictionary

 

* For example , one can consider an employee database in a company which stores the employee details depending upon their seniority that is senior most employee's data may be inserted in the beginning of the database

 

* If the employee's details are stored in a dictionary , then the database will not show the employee details in the same order

 

* When the employee's details are changed , the seniority is disturbed and the data of the employee who joined the company first may not appear in the beginning of the dictionary .

 

* In such a case , the solution is to use ordered dictionaries .

 

* An Ordered Dictionary is a dictionary but it will keep the order of the elements in it . The elements are stored and maintained in the same order as they were entered into the ordered dictionary .

 

* One can create an ordered dictionary using the OrderedDict() method of the collections module . Therefore , we should import the method from the collections module as provided :

 

from collections import OrderedDict

 

Once the ordered dictionary item is created , one can assign an ordered dictionary with the name 'd' as given in the case :

 

d = OrderedDict()

 

One can store the key and values into the ordered dictionary 'd' as in the given manner :

 

d[10] = 'A'

d[11] = 'B'

d[12] = 'C'

d[13] = 'D'

 

Here , 10 is the identifier "Key" and its associated value is "A" . Therefore , the order is not disturbed as 'd' is the ordered dictionary . When we display the key-value pairs from the dictionary 'd' , we can see the same order .

 

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

      Python Program to create a dictionary that

        does not change the order of Elements

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

# create an ordered dictionary from

# collections import OrderedDict

 

d = OrderedDict()

d[10] = 'A'

d[11] = 'B'

d[12] = 'C'

d[13] = 'D'

# display the ordered Dictionary

for i , j in d.items():

    print(i,j)

 

Output

10 - A

11 - B

12 - C

13 - D