Python: OOP and advanced
Classes and inheritance, magic methods, iterators, generators, decorators, memory, threads and processes.
Python's object model and the features agent libraries are built on: classes, inheritance and the special methods behind built-in syntax; iterators, generators and decorators; then how Python frees memory and runs work in threads and processes. Run the notebook cells in order; the last two lessons run scripts from a terminal. Try each practice task before you open its solution.
Lessons
- 1Classes and objectsDefine a class, create objects, give them attributes and methods, and see where each one lives.
- 2InheritanceBuild a class on another, call the parent's initialiser, use two parents, and read the lookup order.
- 3Polymorphism and abstract classesOverride methods, write one function for many classes, rely on duck typing, and require methods with an abstract base class.
- 4EncapsulationPublic, protected and private attributes, name mangling, getters and setters, and properties.
- 5Magic methods and operator overloadingWhat dunder methods are,
__str__and__repr__, containers, operators, comparison and hashing. - 6IteratorsIterables and iterators,
iter()andnext(),StopIteration, what aforloop does, and a class that is its own iterator. - 7Generators
yield, generator objects, pausing and resuming, generator expressions, and reading large files lazily. - 8DecoratorsFunctions as objects, closures, decorators and the
@syntax, decorators with arguments, andfunctools.wraps. - 9Memory managementReference counting,
del, reference cycles and thegcmodule,__del__, habits that keep memory low, andtracemalloc. - 10MultithreadingThreads with
startandjoin, why waiting overlaps but computing doesn't (the GIL), and pools of threads. - 11MultiprocessingProcesses with
startandjoin, the__main__guard, why notebooks can't start them, process pools, and when processes pay off.