OOP Python
OOP Python
Class in Python
Objects in Python
Polymorphism in Python
Encapsulation in Python
Inheritance in Python
Attributes are always public and can be accessed using the dot (.)
operator. Eg.: Myclass.Myattribute
class ClassName:
# Statement-1
.
.
.
# Statement-N
In the above example, we have created a class named Dog using the class
keyword.
# Python3 program to
# demonstrate defining
# a class
class Dog:
pass
Output
Python Objects
To understand the state, behavior, and identity let us take the example of
the class dog (explained above).
Creating an Object
This will create an object named obj of the class Dog defined above.
Before diving deep into objects and classes let us understand some basic
keywords that will be used while working with objects and classes.
obj = Dog()
class Dog:
# class attribute
attr1 = "mammal"
# Instance attribute
def __init__(self, name):
self.name = name
# Driver code
# Object instantiation
Rodger = Dog("Rodger")
Tommy = Dog("Tommy")
Output
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
The driver code starts by creating two instances of the Dog class: Rodger
and Tommy. The __init__ method is called for each instance to initialize
their name attributes with the provided names. The speak method is
called in both instances (Rodger.speak() and Tommy.speak()), causing
each dog to print a statement with its name.
class Dog:
# class attribute
attr1 = "mammal"
# Instance attribute
def __init__(self, name):
self.name = name
def speak(self):
print("My name is {}".format(self.name))
# Driver code
# Object instantiation
Rodger = Dog("Rodger")
Tommy = Dog("Tommy")
Output
My name is Rodger
My name is Tommy
Python Inheritance:
Types of Inheritance:
Inheritance in Python
In the above article, we have created two classes i.e. Person (parent class)
and Employee (Child Class). The Employee class inherits from the Person
class. We can use the methods of the person class through the employee
class as seen in the display function in the above code. A child class can
also modify the behavior of the parent class as seen through the details()
method.
# parent class
class Person(object):
def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
# child class
class Employee(Person):
def __init__(self, name, idnumber, salary, post):
self.salary = salary
self.post = post
def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
print("Post: {}".format(self.post))
Output
Rahul
886012
My name is Rahul
IdNumber: 886012
Post: Intern
Python Polymorphism :
Polymorphism in Python
class Bird:
def intro(self):
print("There are many types of birds.")
def flight(self):
print("Most of the birds can fly but some cannot.")
class sparrow(Bird):
def flight(self):
print("Sparrows can fly.")
class ostrich(Bird):
def flight(self):
print("Ostriches cannot fly.")
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
obj_bird.intro()
obj_bird.flight()
obj_spr.intro()
obj_spr.flight()
obj_ost.intro()
obj_ost.flight()
Output :
Encapsulation in Python
# Python program to
# demonstrate private members
# "__" double underscore represents private attribute.
# Private attributes start with "__".
# Calling constructor of
# Base class
Base.__init__(self)
print("Calling private member of base class: ")
print(self.__c)
# Driver code
obj1 = Base()
print(obj1.a)
Output
GeeksforGeeks
Data Abstraction
class Rectangle:
def __init__(self, length, width):
self.__length = length # Private attribute
self.__width = width # Private attribute
def area(self):
return self.__length * self.__width
def perimeter(self):
return 2 * (self.__length + self.__width)
rect = Rectangle(5, 3)
print(f"Area: {rect.area()}") # Output: Area: 15
print(f"Perimeter: {rect.perimeter()}") # Output: Perimeter: 16
Output
Area: 15
Perimeter: 16
Difference between abstract class and interface in Python?
Method Description
getstate() Returns the current internal state of the random number generator
choices() Returns a list with a random selection from the given sequence
Returns a random float number between two given parameters, you can also set a
triangular()
mode parameter to specify the midpoint between the two other parameters
Returns a random float number between 0 and 1 based on the Beta distribution
betavariate()
(used in statistics)
Returns a random float number based on the von Mises distribution (used in
vonmisesvariate()
directional statistics)