Object Oriented Programming contd
- Today lets practically implement object oriented programming
- Creating classes and objects
- Class
class Point:
pass
- object creation
p = Point()
- In python everything is object, and by default every class is derived from Object
- A class can have 3 types of methods
- instance
- class
- static
- Objects dunder methods
| Category | Method | Triggered By / Used For | Significance / Example |
|---|---|---|---|
| Object Creation & Initialization | __new__(cls, ...) |
Called to create a new instance | Controls instance creation before initialization. <br>🔹 Example: Custom singleton classes. |
__init__(self, ...) |
Called after instance creation | Initializes object attributes. <br>🔹 Example: def __init__(self, name): self.name = name |
|
__del__(self) |
Called before object is destroyed | Defines custom cleanup logic. <br>⚠️ Rarely used; rely on with statements or context managers. |
|
| String Representation | __str__(self) |
Called by str(obj) or print(obj) |
User-friendly string output. <br>🔹 Example: return f"Employee: {self.name}" |
__repr__(self) |
Called by repr(obj) or interpreter |
Developer-friendly representation; should be unambiguous. <br>🔹 Example: return f"Employee({self.name!r})" |
|
__format__(self, format_spec) |
Called by format(obj, spec) or f-strings |
Custom string formatting. | |
| Arithmetic Operations | __add__(self, other) |
obj1 + obj2 |
Defines addition behavior. |
__sub__(self, other) |
obj1 - obj2 |
Defines subtraction behavior. | |
__mul__(self, other) |
obj1 * obj2 |
Defines multiplication behavior. | |
__truediv__(self, other) |
obj1 / obj2 |
Defines division behavior. | |
__floordiv__(self, other) |
obj1 // obj2 |
Floor division. | |
__mod__(self, other) |
obj1 % obj2 |
Modulus operation. | |
__pow__(self, other) |
obj1 ** obj2 |
Power operation. | |
__neg__(self) |
-obj |
Negation (unary minus). | |
| Comparison Operations | __eq__(self, other) |
obj1 == obj2 |
Equality check. |
__ne__(self, other) |
obj1 != obj2 |
Inequality check. | |
__lt__(self, other) |
obj1 < obj2 |
Less than. | |
__le__(self, other) |
obj1 <= obj2 |
Less than or equal. | |
__gt__(self, other) |
obj1 > obj2 |
Greater than. | |
__ge__(self, other) |
obj1 >= obj2 |
Greater than or equal. | |
| Container & Sequence Protocols | __len__(self) |
len(obj) |
Returns length. |
__getitem__(self, key) |
obj[key] |
Retrieve element by key/index. | |
__setitem__(self, key, value) |
obj[key] = value |
Assign element. | |
__delitem__(self, key) |
del obj[key] |
Delete element. | |
__iter__(self) |
iter(obj) |
Return iterator object. | |
__next__(self) |
next(iterator) |
Return next element in iteration. | |
| Attribute Access | __getattr__(self, name) |
Called when attribute not found | Handles dynamic attributes. |
__getattribute__(self, name) |
Called for all attribute accesses | Controls attribute access deeply. | |
__setattr__(self, name, value) |
Assign attribute | Intercept attribute assignments. | |
__delattr__(self, name) |
Delete attribute | Intercept attribute deletions. | |
| Callable Objects | __call__(self, *args, **kwargs) |
obj() |
Make an instance callable like a function. |
| Context Management | __enter__(self) |
Enter with block |
Defines resource setup. |
__exit__(self, exc_type, exc_val, exc_tb) |
Exit with block |
Defines cleanup or exception handling. | |
| Object Introspection | __dir__(self) |
dir(obj) |
Customize attribute listing. |
__sizeof__(self) |
sys.getsizeof(obj) |
Return object’s memory size. | |
| Type Conversion | __int__(self) |
int(obj) |
Convert to integer. |
__float__(self) |
float(obj) |
Convert to float. | |
__bool__(self) |
bool(obj) or if obj: |
Define truthiness of an object. |
-
Refer Here for programiz tutorial
-
In almost all object oriented languages we have a concept of
- public
- private
- protected
-
Python doesnot support any of this directly, but does by convention Refer Here
-
Refer Here for notebook.
