Relative Content

Tag Archive for pythonpython-3.xgenericspython-typing

How to generically type-annotate a fuction/method parameter to accept any real number type?

I would like to write a generic function that takes an instance of any type that implements all real number operations (such as int, float, fractions.Fraction, or third-party types). I found the numbers.Real ABC which works great for enforcing the type at runtime with a isinstance(x, numbers.Real) check, and all built-in real number numeric types in Python are registered as a virtual subclass of it.