0% found this document useful (0 votes)
6 views13 pages

OOP in Simple Words (Step 12)

Uploaded by

mrnobita332211
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)
6 views13 pages

OOP in Simple Words (Step 12)

Uploaded by

mrnobita332211
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/ 13

Sahir Ahmed Sheikh

COMPREHENSIVE GUIDE TO OOP IN PYTHON

1. What Is OOP?
 Definition: OOP (Object-Oriented Programming) is a way to write code by organizing it
into "objects." Objects are like real-world things (e.g., a car, a person) that have
properties (like color, name) and behaviors (like driving, talking). OOP makes code
easier to manage, reuse, and understand.

2. Why use OOP?


 It organizes code better.
 You can reuse code (e.g., use the same "Car" blueprint for many cars).
 It makes complex programs easier to maintain.

Example:
Let’s create a simple "Dog" object.

3. What is a Class?
 Definition: A class is a blueprint for creating objects. It defines what properties (data)
and behaviors (functions) the object will have.

Example:
We already saw a Dog class above. Here’s another example with a Car class.

Page 1 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

4. What is an Object?
 Definition: An object is something you create from a class. It’s like using a blueprint
(class) to build an actual thing (object).

Example:
In the above code, my_car is an object of the Car class.

5. Defining Attributes and Methods


 Attributes: These are the properties of an object (like name, brand).
 Methods: These are the functions (behaviors) inside a class, like bark() or drive().

Example:
Let’s add more attributes and methods to a Student class.

6. Instantiating Objects
 Definition: Instantiating means creating an object from a class. You use the class name
with parentheses, like Student(“Ali”, 20).

Example:
We’ve already instantiated objects like my_dog, my_car, and student1 in the examples
above.

7. The Self Keyword


 Definition: self refers to the object itself. It’s used inside a class to access its own
attributes and methods.

Example:
In the Student class, self.name means "the name of this student object."

Page 2 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

8. What is a Constructor?
 Definition: A constructor is a special method called __init__ that runs automatically
when you create an object. It’s used to set up the object’s initial attributes.

Example:
The __init__ method in all the examples above is a constructor.

9. Parameterized Constructors
 Definition: A constructor that takes parameters (like name, age) to initialize the
object’s attributes.

Example:
The Student class above has a parameterized constructor: def __init__(self, name, age).

10. Default Constructors


 Definition: If you don’t define an __init__ method, Python gives a default constructor
that doesn’t take any parameters (except self).

Example:

Page 3 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

11. The __del__ Method (Destructor)


 Definition: A destructor is a special method called __del__ that runs when an object is
deleted (when it’s no longer deleted).

Example:

12. Class Attributes and Instance Attributes


 Class Attributes: Shared by all objects of a class.
 Instance Attributes: Unique to each object.
Example:

13. The __dict__ Attribute


 Definition: __dict__ is a dictionary that shows all the attributes of an object and class.
Example:

Page 4 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

14. Methods In Python Classes


 Instance Methods: Work with an object (use self ).
 Class Methods: Work with the class (use @classmethod ).
 Static Methods: Don’t need the object or class (use @staticmethod ).
Example:

15. Key Principles Of OOP


 Encapsulation
 Inheritance
 Polymorphism
 Abstraction

1. Encapsulation
 It refers to wrapping up data and methods into a single unit (a class) and restricting
direct access to some of an object's components.
 Think of it like: A capsule that contains both medicine (data) and instructions
(methods) inside it — you can’t directly access what's inside; you follow the provided
interface (e.g., a method) to use it.
 Access Modifiers: Public (normal), Private (with __ ), Protected (with _ ).

Page 5 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

Example:

 Using __init__ for Access Control


Definition: Use __init__ to set up private or protected attributes.

Example:
We already did this in the BankAccount example above.

 Getters And Setters Methods


Definition: Getters get the value of an attribute, and setters set the value, often with
some checks.

Example:

Page 6 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

2. Inheritance
 Definition: Inheritance lets a class (child) take properties and methods from another
class (parent).
 Types Of Inheritance:
 Single Inheritance: One parent, one child.
 Multi-level Inheritance: A chain (Parent → Child → Grandchild).
 Multiple Inheritance: One child with multiple parents.

Example: (Single Inheritance)

Example: (Multi-level Inheritance)

Page 7 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

Example: (Multiple Inheritance)

 The super() Function


Definition: super() lets you call methods from the parent class.

Example:

3. Polymorphism
 Definition: Polymorphism means “many forms”. It lets different classes have methods
with the same name but different behaviors.
 Types Of Polymorphism In Python
 Method Overriding: Child class changes a parent’s method.
 Operator Overloading: Changing how operators (like +) work.
 Duck Typing: If an object behaves like a type, it’s treated as that type.

Page 8 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

Example: (Method overriding)

Example: (Operator Overloading)

Example: (Duck Typing)

Page 9 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

4. Abstraction
 Definition: Abstraction hides complex details and shows only the necessary parts. In
Python, you use abstract classes to define methods that must be implemented by child
classes.
 Key Components:
 ABC: Abstract Base Class (from the abc module) makes a class abstract.
 @abstractmethod : Marks a method as abstract (must be implemented by
child classes).
 Child Classes: Must implement all abstract methods.

Example:

16. The Object Class


 In Python, everything is an object, and every class inherits from the base class object
(either directly or indirectly).
 So, when you write:

…it’s actually like:

Page 10 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

 This means your class inherits some built-in behaviors from the object class (e.g., how
it’s compared, how it looks when printed, etc.).

17. Python’s Unified Type System


 Definition: Python has a unified type system which means, Everything in Python is an
object, and all objects are instances of classes that ultimately inherit from the base
class object.
 What Does This Mean?
In many programming languages:
 Primitive types (like integars, booleans, floats) are not treated as objects.
 But in Python, even these are objects with methods and properties.

Examples of Unified Type System in Action:

All of these are objects, and inherit from the root object class.

Benefits of a Unified Type System

Benefit Explanation

Consistency You can treat everything the same (objects).

Polymorphism Different objects can be used interchangeably in functions.

Introspection You can examine any object at runtime using type(), dir(), etc.

Everything stored as an object reference, simplifying memory


Memory Model
handling.

Page 11 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

Real-World Example:

This loop works seamlessly because everything is an object and can be handled uniformly.

18. Special (Magic/Dunder) Methods


 These are special methods in Python surrounded by double underscores (e.g., __init__,
__str__). They allow you to define how objects behave with built-in operations.

Common Dunder Methods:

Dunder Method Purpose Example Usage

Constructor (called when object is


__init__ obj = MyClass()
created)

String representation (used in


__str__ print(obj)
print(obj))

Developer-focused string
__repr__ repr(obj)
representation

__len__ Length of object len(obj)

__getitem__ Indexing like obj[key] obj[0]

__setitem__ Setting value like obj[key] = value obj[0] = 10

__eq__ Equality check obj1 == obj2

__lt__, __gt__ Less than / Greater than obj1 < obj2, obj1 > obj2

__call__ Makes object callable like a function obj()

Destructor (called when object is


__del__ del obj
deleted)

Page 12 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON

Example:

Why Use Dunder Methods?


 They allow custom classes to behave like built-in types, making your code more
Pythonic and powerful.

Page 13 of 13

You might also like