Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 1.54 KB

File metadata and controls

31 lines (26 loc) · 1.54 KB

Python OOP — magic methods by example

CI

Sixteen tiny, numbered, runnable files — each demonstrates one dunder / OOP mechanism, including the failure cases most tutorials skip.

File Topic
1_math_magic.py Arithmetic dunders (__add__ & co.) on your own class
2_setitem.py __setitem__ — dict-like assignment
3_getitem.py __getitem__ — indexing and slicing
4_iter.py Making an object iterable with __iter__
5_custom_iterator.py Iterator protocol by hand (__next__, StopIteration)
6_custom_iterable.py Iterable vs iterator — why they're different objects
7_math_magic_usage.py Using the arithmetic class from file 1
8_callable.py __call__ — objects you can invoke
9_attributes.py __getattr__ / attribute lookup order
10_static_method.py @staticmethod basics
11_static_method_create.py Static factory methods
12_static_method_fail.py Where a static method is the wrong tool (deliberate failure)
13_classmethod.py @classmethod and cls-based construction
14_context_manager.py __enter__/__exit__ and @contextmanager
15_slots.py __slots__ — memory trade-off and the dynamic-attribute limitation
16_property.py @property — validation in setters, computed read-only attributes

Run any file directly:

python 5_custom_iterator.py