Introspecting Python Functions
Contents
Functions are objects
Since everything in Python is an object so are functions. They are instances of class <class 'function'>
and have properties and methods attached to them.
Let’s define a simple function and play with its properties:
|
|
Building a type validator for function arguments
One interesting experiment I did was to create a very simple type checker that validates values against the annotated types using function introspection techniques.
Our type checker is a decorator that is applied to a function with type annotations:
|
|
Let’s run the square
methods with some inputs:
|
|
Author naeem91
LastMod 2022-09-13