Agentic AI

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

  1. 1Classes and objectsDefine a class, create objects, give them attributes and methods, and see where each one lives.
  2. 2InheritanceBuild a class on another, call the parent's initialiser, use two parents, and read the lookup order.
  3. 3Polymorphism and abstract classesOverride methods, write one function for many classes, rely on duck typing, and require methods with an abstract base class.
  4. 4EncapsulationPublic, protected and private attributes, name mangling, getters and setters, and properties.
  5. 5Magic methods and operator overloadingWhat dunder methods are, __str__ and __repr__, containers, operators, comparison and hashing.
  6. 6IteratorsIterables and iterators, iter() and next(), StopIteration, what a for loop does, and a class that is its own iterator.
  7. 7Generatorsyield, generator objects, pausing and resuming, generator expressions, and reading large files lazily.
  8. 8DecoratorsFunctions as objects, closures, decorators and the @ syntax, decorators with arguments, and functools.wraps.
  9. 9Memory managementReference counting, del, reference cycles and the gc module, __del__, habits that keep memory low, and tracemalloc.
  10. 10MultithreadingThreads with start and join, why waiting overlaps but computing doesn't (the GIL), and pools of threads.
  11. 11MultiprocessingProcesses with start and join, the __main__ guard, why notebooks can't start them, process pools, and when processes pay off.