Object-Oriented Programming in Python3
We can think as though we're working with real-life items or objects when we use an object-oriented programming approach.
Python having many kind of datatype such as int, float, string etc. In python everything is an object.
Every objects having type and methods.
Object: object is an run time entity. It is an instance of a particular type.
f=2.3,4.5,2.1 so every values of float is going to store as an instance.
Class Keyword : class provides a blueprint or a template using which objects are created.
A class creates a new local namespace where all its attributes( data and functions) are defined.
In above class structure colon (:) define the class block
Atributes : rollnumber and name
init() : constructor of class
self parameter : refers to the newly created instance of the class.66060 60 0 60
attributes rollnumber and name are associated with self-keyword to identify them as instance variables.
Creating Object :
Once the class is created , next job is to create an object or an instance of the class. Object can be created by class name of the class followed by parenthesis.
Example creating an object of student:
parametrized constructor - dynamically assign the attribute values during object creation
Class Variable and Instance variables
Adding a method to class
Length = 10
Breadth = 5
Area = 50
Significance of self:
Length = 15
Breadth = 8
Area = 120
Length = 10
Breadth = 5
Area = 50
Class Method and Static Method
This is circle's static method
Inheritance and Overriding
Circle with radius = 5 when colored red costs $ 157.0
Simple Animal Class Creation:
Making Elephant Function Call
Roll Number : 12345
Name : Ram Krishn
Roll Number : 456789
Name : Mishra
Total Students= 2
ACCESS MODIFIERS (PUBLIC, PRIVATE AND PROTECTED) in Python
Python doesn't have any mechanism that effectively restricts access to any instance variable or method. Python prescribes a convention of prefixing the name of the variable/method with single or double underscore to emulate the behaviour of protected and private access specifiers.
All members in a Python class are public by default. Any member can be accessed from outside the class environment.
INHERITANCE in Python
Inheritance is a mechanism in which one class (derived class) acquires the property of another class (base class). With inheritance, we can reuse the variables and methods of the existing class. The existing class is called base class and the new class is called derived class.
Hence, inheritance facilitates reusability and is an important concept of object oriented programming.
Types of inheritance
Single inheritance enables a derived class to use the variables and functions defined in an existing class. In multiple inheritance, derived class inherits the characteristics and features from more than one existing classes.
In python, syntax for defining single inheritance is class z(x), where x is the name of the base class and z is the name of the derived class. Similarly, multiple inheritance is defined using the syntax class z(x, y), where x and y are the names of base classes and z is the name of the derived class.
Advantages of inheritance
Inheritance is the capability of one class to derive or inherit the properties from some another class. The benefits of inheritance are:
It represents real-world relationships well.
It provides reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A
Designation: Data analyst
Room no.: 203
Operator overloading allow us to redefine the way operator works for user-defined types such as objects. It cannot be used for built-in types such as int, float, char etc., For example, '+' operator can be overloaded to perform addition of two objects of distance class.
Python provides some special function or magic function that is automatically invoked when it is associated with that particular operator. For example, when we use + operator on objects, the magic method add () is automatically invoked in which the meaning/operation for + operator is defined for user defined objects.
In Python you can define a method in such a way that there are multiple ways to call it. Depending on the function definition, it can be called with zero, one, two or more parameters.
Method Overloading Example
Enter feet and inches of distance1: 10 25
Enter feet and inches of distance2: 45 32
Distance2 is greater or equal to Distance1
Sum of the two Distance is:
Feet= 56 Inches= 45
METHOD OVERRIDING IN PYTHON
Overriding is the ability of a class to change the implementation of a method provided by one of its ancestors. Overriding is a very important part of OOP since it is the feature that makes inheritance exploit its full power. Through method overriding a class may "copy" another class, avoiding duplicated code, and at the same time enhance or customize part of it. Method overriding is thus a strict part of the inheritance mechanism.
As for most OOP languages, in Python inheritance works through implicit delegation: when the object cannot satisfy a request, it first tries to forward the request to its ancestors, following the specific language rules in the case of multiple inheritance.