15 Introspection and Generics

Introspection is the ability of code to access information about itself at runtime and respond accordingly. As an interpreted language, Python excels at introspection. By understanding how Python inspects objects, you can uncover a number of patterns for improving and optimizing your code.

In this chapter, I’ll cover special attributes, which make this introspection possible. By making use of these special attributes, I’ll cover generic functions, descriptors, and slots, and I’ll even build an (effectively) immutable class. Then, while on the topic of code running itself, I’ll touch on the dangers of arbitrary ...

Get Dead Simple Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.