0% found this document useful (0 votes)
4 views3 pages

OOP in Python Notes

The document provides comprehensive notes on Object-Oriented Programming (OOP) in Python, covering key concepts such as classes, objects, encapsulation, inheritance, polymorphism, abstraction, and special methods. It includes code examples to illustrate each concept and emphasizes best practices for implementing OOP. Overall, it serves as a guide for understanding and applying OOP principles in Python programming.

Uploaded by

assamirzafar62
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)
4 views3 pages

OOP in Python Notes

The document provides comprehensive notes on Object-Oriented Programming (OOP) in Python, covering key concepts such as classes, objects, encapsulation, inheritance, polymorphism, abstraction, and special methods. It includes code examples to illustrate each concept and emphasizes best practices for implementing OOP. Overall, it serves as a guide for understanding and applying OOP principles in Python programming.

Uploaded by

assamirzafar62
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

OOP in Python - Full Notes

1. What is OOP?

OOP is a way of structuring code around objects: bundles of data (attributes) and behavior (methods).
Example: Car object has attributes like color, speed and methods like drive(), brake().

2. Class and Object

Class is a blueprint. Object is an instance of that blueprint.

class Dog:
def bark(self):
print("Woof!")

d = Dog()
[Link]()

3. __init__ Method

Constructor called when object is created. Used to initialize attributes.

class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age

p = Person("Ali", 22)
print([Link])

4. Encapsulation

Hides internal details. Public (name), Protected (_name), Private (__name)

class BankAccount:
def __init__(self):
[Link] = 1000
self._currency = "PKR"
self.__pin = 1234

acc = BankAccount()
print([Link])
print(acc._currency)
print(acc._BankAccount__pin)

5. Inheritance

One class inherits from another. Can override or extend functionality.

class Animal:
OOP in Python - Full Notes

def speak(self):
print("Animal sound")

class Dog(Animal):
def speak(self):
print("Woof!")

d = Dog()
[Link]()

6. Polymorphism

Same method name, different behavior in different classes.

class Cat:
def sound(self):
print("Meow")

class Dog:
def sound(self):
print("Woof")

for animal in [Cat(), Dog()]:


[Link]()

7. Abstraction

Hide internal details and force subclasses to implement essential methods.

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

class Circle(Shape):
def area(self):
return 3.14 * 5 * 5

8. Special Methods

Used to define how objects behave with built-in functions like print(), len().

class Book:
def __init__(self, title):
[Link] = title

def __str__(self):
OOP in Python - Full Notes

return f"Book: {[Link]}"

def __len__(self):
return 300

b = Book("Python")
print(b)
print(len(b))

9. Class vs Instance Attributes

Class attributes are shared. Instance attributes are unique per object.

class Counter:
count = 0
def __init__(self):
[Link] += 1

c1 = Counter()
c2 = Counter()
print([Link])

10. OOP Best Practices

- Use self for instance data


- Use _ and __ for protected/private
- Use @property for controlled access
- Use abc for abstract templates
- Prefer simple design when possible

You might also like