Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Python OOP's

Python allows for object-oriented programming with classes and objects. Major OOP principles in Python include classes, objects, methods, inheritance, polymorphism, encapsulation, and abstraction. A class is like a blueprint that defines properties and behaviors common to all objects of that class. An object is an instance of a class with unique property values. Methods are functions defined inside a class that act on object properties.

Uploaded by

shivamgaming7091
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Python OOP's

Python allows for object-oriented programming with classes and objects. Major OOP principles in Python include classes, objects, methods, inheritance, polymorphism, encapsulation, and abstraction. A class is like a blueprint that defines properties and behaviors common to all objects of that class. An object is an instance of a class with unique property values. Methods are functions defined inside a class that act on object properties.

Uploaded by

shivamgaming7091
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python OOPs Concepts

Like other general-purpose programming languages, Python is also an object-oriented


language since its beginning. It allows us to develop applications using an Object-Oriented
approach. In Python, we can easily create and use classes and objects.

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.

Major principles of object-oriented programming system are given below.

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.

Python Classes and Objects

Python is an object oriented programming language.


Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.

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)

The __init__() Function


The examples above are classes and objects in their simplest form, and are not really useful in
real life applications.
To understand the meaning of classes we have to understand the built-in __init__() function.
All classes have a function called __init__(), which is always executed when the class is
being initiated.
Use the __init__() function to assign values to object properties, or other operations that are
necessary to do when the object is being created:

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.

The self Parameter


The self parameter is a reference to the current instance of the class, and is used to access
variables that belongs to the class.
It does not have to be named self, you can call it whatever you like, but it has to be the first
parameter of any function in the class:
Example
Use the words mysillyobject and abc instead of self:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age

def myfunc(abc):
print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc()

Modify Object Properties


You can modify properties on objects like this:
Example
Set the age of p1 to 40:
p1.age = 40

Delete Object Properties


You can delete properties on objects by using the del keyword:
Example
Delete the age property from the p1 object:
del p1.age

Delete Objects
You can delete objects by using the del keyword:
Example
Delete the p1 object:
del p1

The pass Statement


class definitions cannot be empty, but if you for some reason have a class definition with no
content, put in the pass statement to avoid getting an error.
Example
class Person:
pass

How to import a class?


Getting Started
Here we have created a class named GFG which has two methods: add() and sub(). Apart
from that an explicit function is created named method() in the same python file. This file
will act as a module for the main python file.

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")

Let the name of the above python file be module.py.

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

# Created a class object


object = module.GFG()
# Calling and printing class methods
print(object.add(15,5))
print(object.sub(15,5))
# Calling the function
module.method()

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

You might also like