Python OOP's
Python OOP's
An object-oriented paradigm is to design the program using classes and objects. The object is
related to real-word entities such as book, house, pencil, etc. The oops concept focuses on
writing the reusable code. It is a widespread technique to solve the problem by creating
objects.
o Class
o Object
o Method
o Inheritance
o Polymorphism
o Data Abstraction
o Encapsulation
Class
The class can be defined as a collection of objects. It is a logical entity that has some specific
attributes and methods. For example: if you have an employee class, then it should contain an
attribute and method, i.e. an email id, name, age, salary, etc.
Syntax
class ClassName:
<statement-1>
.
.
<statement-N>
Object
The object is an entity that has state and behavior. It may be any real-world object like the
mouse, keyboard, chair, table, pen, etc.
Everything in Python is an object, and almost everything has attributes and methods. All
functions have a built-in attribute __doc__, which returns the docstring defined in the
function source code.
When we define a class, it needs to create an object to allocate the memory.
Method
The method is a function that is associated with an object. In Python, a method is not unique
to class instances. Any object type can have methods.
Create a Class
To create a class, use the keyword class:
Example
Create a class named MyClass, with a property named x:
class MyClass:
x=5
Create Object
Now we can use the class named MyClass to create objects:
Example
Create an object named p1, and print the value of x:
p1 = MyClass()
print(p1.x)
Example
Create a class named Person, use the __init__() function to assign values for name and age:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Note: The __init__() function is called automatically every time the class is being used to
create a new object.
The __str__() Function
The __str__() function controls what should be returned when the class object is represented
as a string.
If the __str__() function is not set, the string representation of the object is returned:
Example
The string representation of an object WITHOUT the __str__() function:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1)
Output:
<__main__.Person object at 0x00000268BA847630>
Example
The string representation of an object WITH the __str__() function:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}({self.age})"
p1 = Person("John", 36)
print(p1)
Output:
John(36)
Object Methods
Objects can also contain methods. Methods in objects are functions that belong to the object.
Let us create a method in the Person class:
Example
Insert a function that prints a greeting, and execute it on the p1 object:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
Note: The self parameter is a reference to the current instance of the class, and is used to
access variables that belong to the class.
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Delete Objects
You can delete objects by using the del keyword:
Example
Delete the p1 object:
del p1
class GFG:
# methods
def add(self, a, b):
return a + b
def sub(self, a, b):
return a - b
# explicit function
def method():
print("GFG")
Importing
It’s now time to import the module and start trying out our new class and functions. Here,
we will import a module named module and create the object of the class named GFG
inside that module. Now, we can use its methods and variables.
import module
Output:
20
10
GFG
Importing the module as we mentioned earlier will automatically bring over every single
class and performance within the module into the namespace. If you’re only getting to use
one function, you’ll prevent the namespace from being cluttered by only importing that
function as demonstrated in the program below:
# import module
from module import method
# call method from that module
method()
Output:
GFG