oop
#oop(OOP) modern tutorial
1. oop overview
oop (Object‑Oriented Programming, OOP) is a programming paradigm with "object interaction" as its core. It packages data (properties) and logic (methods) for operating data into an independent individual - an object. In this way, the code is organized in a way that is closer to human understanding of the world. For example, think of "student" as an entity with name, score attributes, and actions such as "hand in homework" and "print results", rather than variables and functions scattered around.
1.1 Comparison with procedural programming
In order to intuitively feel the advantages of OOP, we use a small example of performance management to compare the two paradigms:
2. OOP basics in Python
Python is a dynamic language that fully supports OOP, but it does not force you to use OOP. Small scripts can continue to maintain a process-oriented style, while OOP is highly recommended for larger projects to manage complexity.
2.1 Classes and Objects
In Python:
- Class: Create a "template" or "blueprint" for an object, which specifies what properties and methods the object should have.
- Object: A specific individual instantiated by a class, such as "Zhang San" and "Li Si" created through the "Student Template".
Basic class definition
2.2 Creating and using objects
💡 Tips: All methods in Python must explicitly receive
selfParameters, but you don’t need to pass them in yourself when calling, Python will handle it automatically.
3. Three core features of object-oriented
The three major characteristics are the key to distinguishing OOP from other paradigms, and they are also frequently tested in interviews.
3.1 Encapsulation
Encapsulation has two meanings:
- Data Binding: Package properties and methods in the same class.
- Information Hiding: Only necessary interfaces are exposed to the outside world, and internal implementation details (such as the calculation logic of account balances) are not allowed to be modified at will.
Python uses double underscore prefix__ to simulate private properties/methods (actually a name mangling:_类名__属性名, not really private, but enough to express the "no direct access" intent).
Encapsulation example: bank account
by only exposingdeposit、withdrawandget_balance, we can ensure that the balance will not be tampered with by external code at will, and all changes will be legally checked.
3.2 Inheritance
Inheritance is an "is‑a" relationship in which subclasses automatically obtain the properties and methods of the parent class and can:
- Override the method of the parent class;
- Added subclass-specific attributes and methods.
Python supports multiple inheritance, but you need to be careful when using it to avoid the confusion caused by "diamond inheritance".
Inheritance example: Person → Student
🔑 Keywords
super(): Allows you to easily call the method of the parent class, whether during initialization or in a normal method.
3.3 Polymorphism
Polymorphism literally means "multiple forms", and the core idea is: Different objects can make their own responses to the same message (method call). Three prerequisites for achieving polymorphism:
- The parent class defines a public interface (which can be a common method or an abstract method);
- The subclass inherits the parent class and rewrites the interface;
- The caller only relies on the parent class type and does not care which subclass it is.
Polymorphic example: animal sounds
4. Python OOP advanced-features
Python provides some syntactic sugar to make OOP code more concise and efficient.
4.1 Class methods and static methods
In addition to ordinary instance methods, Python has two special methods:
4.2 Property Decorator@property
use@propertyMethod calls can be converted into syntax like accessing properties, which not only maintains encapsulation but also improves readability.
Example: radius and area of a circle
Now you can use them like normal properties:circle.radius、circle.area, while the interior remains protected.
4.3 Abstract Base Class (ABC)
The interface constraint of an ordinary parent class is to "report an error at runtime", while the Abstract Base Class (ABC) can detect errors during instantiation and force the subclass to implement the specified abstract method.
ifRectangleIf an abstract method is omitted, Python will directly report an error, thus exposing the problem in advance.
5. Modern Python OOP Best Practices
After mastering the basic syntax of OOP, the following practices can help you write more robust and professional code.
-
Prefer composition to inheritance Inheritance means "is one" (student → person), combination means "has one" (car → engine). The combination is less coupled and more flexible.
-
Use data classes
@dataclassSimplify entity classes (Python3.7+) it will automatically generate__init__、__repr__、__eq__and other common methods, allowing you to focus on the data itself. -
Always add type hints Type hints not only improve readability but also allow the IDE to provide smart completion and static checking.
-
Follow SOLID design principles Here are the golden rules of large-scale project design (easy to understand):
- S Single responsibility: A class does only one thing.
- O Opening and closing principle: open to extensions, closed to modifications.
- L Liskov replacement: Subclasses can seamlessly replace parent classes.
- I Interface isolation: only provide the interfaces required by the caller.
- D Dependency inversion: rely on abstract interfaces rather than concrete implementations.
6. Summary
oop is the cornerstone of modern software development, and Python provides powerful and flexible support for it. The keys to mastering OOP are:
- Clarify the relationship between classes (blueprints) and objects (instances);
- Make flexible use of the three major features of encapsulation, inheritance, and polymorphism;
- Make good use of Python’s unique syntactic sugar (such as
@property、@dataclass、ABC); - Follow composition-first, type-hint, and SOLID principles in actual projects.
When you can naturally map real-world business models into object interactions, the code is no longer a cold collection of instructions, but a dynamic and collaborative widget. I hope this tutorial can give you a clear and practical understanding of Python OOP, and give you an extra "weapon" when coding.

