Python OOP
Python OOP
COM
Python OOP
by programmingvalley.com
Let’s Swipe
programmingvalley.com 01
What Is OOP?
Object-Oriented Programming
organizes code into objects.
Each object represents a real-world
thing with data (attributes) and actions
(methods).
Let’s Swipe
programmingvalley.com 02
Core Concepts
1. Class and Object
Class = blueprint (e.g. Dog)
Object = instance (e.g. my_dog)
Let’s Swipe
programmingvalley.com 03
Core Concepts
2. __init__ Method
Special method to initialize an
object
Runs automatically when you
create an object
Let’s Swipe
programmingvalley.com 04
Core Concepts
3. Instance Attributes
Belong to the object
Defined with self. inside methods
Let’s Swipe
programmingvalley.com 05
Core Concepts
4. Instance Methods
Functions inside the class
Always take self as first argument
Let’s Swipe
programmingvalley.com 06
Core Concepts
5. Inheritance
Create new class based on an
existing one
Use the parent class features
Let’s Swipe
programmingvalley.com 07
Core Concepts
6. super() Function
Call parent class method in child
class
Let’s Swipe
programmingvalley.com 08
Core Concepts
7. Class Attributes
Shared across all objects
Let’s Swipe
programmingvalley.com 09
Core Concepts
8. Magic Methods
Built-in methods with double
underscores
Examples:
__str__ – string representation
__len__ – define length
__add__ – define + behavior
Let’s Swipe
programmingvalley.com 10
Core Concepts
9. Encapsulation
Keep data private inside class
Use _ or __ for convention
Let’s Swipe
programmingvalley.com 11
Core Concepts
10. Composition (Has-A Relationship)
Use objects inside other objects
Let’s Swipe
programmingvalley.com 12
Tips
Use self for instance access
Use inheritance for related classes
Use composition for loosely related
logic
Use class attributes sparingly
Favor readability over clever tricks
Let’s Swipe
PROGRAMMINGVALLEY.COM
Follow us
to get more
information
and tips
like these.
by programmingvalley.com
Save