0% found this document useful (0 votes)
15 views6 pages

Python OOP: OOP Object-Oriented Programming

Python 5th sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Python OOP: OOP Object-Oriented Programming

Python 5th sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python OOP

What is OOP?
OOP stands for Object-Oriented Programming.
Python is an object-oriented language, allowing you to
structure your code using classes and objects for better
organization and reusability.

Advantages of OOP
• Provides a clear structure to programs
• Makes code easier to maintain, reuse, and debug
• Helps keep your code DRY (Don't Repeat Yourself)
• Allows you to build reusable applications with less
code
Tip: The DRY principle means you should avoid writing
the same code more than once. Move repeated code
into functions or classes and reuse it.

What are Classes and Objects?


Classes and objects are the two core concepts in
object-oriented programming.
A class defines what an object should look like, and an
object is created based on that class. For example:
Class Objects

Fruit Apple, Banana, Mango

Car Volvo, Audi, Toyota

When you create an object from a class, it inherits all the variables
and functions defined inside that class.
In the next chapters, you will learn how to:
• Create a class
• Create objects
• Access class members

• And much more

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
print(MyClass)

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:
class MyClass:
x=5
p1 = MyClass()
print(p1.x)

Example:
class MyClass:
x=5

p1 = MyClass()
print(p1.x)

The __init__() Method


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__() method.
All classes have a method called __init__(), which is always executed
when the class is being initiated.
Use the __init__() method 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__() method to assign
values for name and age:
class Person:
def __init__(self, name, age,address):
self.name = name
self.age = age
self.address=address

p1 = Person("John", 36,”mawlai”)

print(p1.name)
print(p1.age)

Create Methods
You can create your own methods inside objects. 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)
p1.myfunc()

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.

You might also like