Dynamically generated methods with a non-generic signature
summary
Generates per-subclass setter methods for scikit-learn Estimators by inspecting child method signatures (so a custom set_transform_request appears only if transform defines extra params), attaches callable via a descriptor at subclass creation, and writes a matching PEP-362 Signature and docstring so help() and IDE introspection show precise params. Uses inspect to read signatures, descriptors to return functions, and __init_subclass__ (PEP-487) to attach them; approach is deployed in scikit-learn and works at import time.
AI-written summary · about
chapters (12)
- 0:00Introduction and motivation
- 3:43Understanding inspect module
- 6:33Functions returning functions
- 10:10Python signature object
- 10:30Descriptors in Python
- 14:07Descriptor returning a function
- 17:21Init subclass explained
- 20:35Putting it all together
- 23:57Maintainability and black magic
- 27:10Documentation and IDE support
- 28:13Language servers and type checkers
- 30:00Q&A and closing
related talks
29mGetting Scikit-Learn To Run On Top Of Pandas
45mDataclasses: The code generator to end all code generators
27mSoftware Library APIs: Lessons Learned from scikit-learn
29mMetaprogramming with Decorators, Metaclasses, and Dynamic Code Generation
25mThe magic of "self": How Python inserts "self" into methods.
30mMetaclasses Demystified
30mThe Wizardry of Metaprogramming
33mMetaprogramming in Python
