0% found this document useful (0 votes)
2 views2 pages

OOP Python Program Example (1)

Uploaded by

vishtell57
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)
2 views2 pages

OOP Python Program Example (1)

Uploaded by

vishtell57
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/ 2

OOP-based Python Program Example

Object-Oriented Programming (OOP) in Python allows developers to structure code using objects

and classes. Here's an example of a simple OOP-based program using and not using the special

method __init__.

1. With __init__ Method


class Car:

def __init__(self, brand, model):

self.brand = brand

self.model = model

def display_info(self):

return f"Car: {self.brand} {self.model}"

car1 = Car("Toyota", "Camry")

print(car1.display_info()) # Output: Car: Toyota Camry

2. Without __init__ Method


class Bike:

def set_info(self, brand, model):

self.brand = brand

self.model = model

def display_info(self):

return f"Bike: {self.brand} {self.model}"

bike1 = Bike()

bike1.set_info("Honda", "CBR")

print(bike1.display_info()) # Output: Bike: Honda CBR

Pros and Cons of Using OOP in Python


Pros:

- Code is organized, reusable, and modular.


- Makes complex programs easier to manage.

- Encourages data encapsulation and abstraction.

- Easy to represent real-world entities.

Cons:

- Can be overkill for small scripts.

- Slightly more memory usage due to objects.

- Initial learning curve for beginners.

- More boilerplate code compared to procedural approach.

You might also like