Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which bundle state (attributes/data) and behavior (methods/functions) together. In Python, everything is an object—from simple integers and strings to complex modules and custom classes.
A class serves as a blueprint or schema defining the attributes and operations that instances will possess. An instance (or object) is a concrete manifestation created in memory based on that blueprint.
__init__ Constructor and selfThe __init__ method is Python's initialization hook called immediately after an instance is created in memory:
self: A reference to the specific instance currently being initialized or manipulated. Python automatically passes the instance as the first argument when calling instance methods.Understanding the distinction between class-level and instance-level attributes is critical to avoiding state bugs:
self. Each instance maintains its own distinct copy in its __dict__.Python provides three distinct method types using decorators:
| Method Type | Decorator | First Argument | Typical Use Case |
|---|---|---|---|
| Instance Method | (None) | self | Manipulates individual instance state |
| Class Method | @classmethod | cls | Factory methods, modifying class-level state |
| Static Method | @staticmethod | (None) | Self-contained utility functions logically grouped in the class |
__str__ vs __repr__Every Python class should define informative string representations:
__str__: User-friendly, readable string representation (returned by print(obj) or str(obj)).__repr__: Unambiguous, developer-oriented string used for debugging (returned by typing obj in interactive shell or repr(obj)). Ideally should look like valid Python code to recreate the object.A mutable class attribute (like a list) is shared across all instances of the class!
self in Method SignaturesEvery instance method must take self as its first parameter; omitting it causes a TypeError: method takes 0 positional arguments but 1 was given.
Create an Item class for an e-commerce store with attributes name, price, and quantity. Implement methods total_value(), apply_discount(pct), and a formatted __repr__() method.
self parameter in a Python instance method represent?cls?@staticmethod differ from regular instance methods and class methods?__repr__ special method in Python?In the next lesson, we will continue your intermediate Python journey with Inheritance & Method Overriding (5: Object-Oriented Programming (Intermediate)).
👉 Continue to Next Lesson: Inheritance & Method Overriding →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Project: CSV Contact Manager | Inheritance & Method Overriding |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.