What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of 'objects', which can contain data (attributes) and code (methods). Python is a multi-paradigm language, but it is heavily built around OOP principles. Everything in Python—from strings to functions—is actually an object.
Classes and Objects
A Class is a blueprint for creating objects. An Object is an instance of a class. The __init__ method is the constructor that initializes an object's attributes when it is created.
The Four Pillars of OOP
- Encapsulation: Bundling data and methods that work on that data within one unit and restricting access to some components (using underscores like
_nameor__name). - Inheritance: Allowing a class (child) to derive attributes and methods from another class (parent).
- Polymorphism: Allowing different classes to be treated as instances of the same general class through the same interface (e.g., different objects having a
render()method). - Abstraction: Hiding complex implementation details and showing only the necessary features of an object.
Inheritance Example
Inheritance allows for code reuse. You can use the super() function to call methods from the parent class.
OOP Comparison: Python vs. Dart
| Feature | Python | Dart (Flutter) |
|---|---|---|
| Constructor | __init__(self, ...) | ClassName(...) |
| Instance Reference | self | this |
| Access Modifiers | Naming convention (_ or __) | Keywords (private is _) |
| Multiple Inheritance | Supported | Not supported (uses Mixins) |
| Interfaces | Abstract Base Classes (abc) | Implicit (every class is an interface) |