Python Universal Infinity Value
The `infinity` library for Python provides a universal, all-in-one infinity value (`inf`) that can be compared to any other object, including numbers, lists, strings, and custom types, offering more flexible comparison behavior than `float('inf')`. It is currently at version 1.5 and has an infrequent release cadence for its stable feature set, reflecting its simple and focused utility.
Common errors
-
NameError: name 'inf' is not defined
cause The `inf` object was not correctly imported from the `infinity` library.fixAdd `from infinity import inf` at the top of your Python script or module where you intend to use `inf`. -
AttributeError: module 'infinity' has no attribute 'Infinity'
cause Attempting to instantiate a class named `Infinity` (e.g., `infinity.Infinity()`) instead of using the pre-defined singleton `inf`.fixThe `inf` object is a singleton and should be imported and used directly: `from infinity import inf; my_val = inf`. -
TypeError: '<' not supported between instances of 'float' and 'str'
cause You are likely using `float('inf')` or `math.inf` for comparison against a non-numeric type (e.g., string, list). Standard Python floats do not support such comparisons.fixSwitch to `infinity.inf` for universal comparisons across different data types. Ensure you have `from infinity import inf` and use `inf` for these broader comparisons.
Warnings
- gotcha Do not confuse `infinity.inf` with `float('inf')` or `math.inf`. While `infinity.inf` is equal to `float('inf')`, it offers enhanced comparison capabilities against non-numeric types that standard Python floats do not support.
- gotcha `infinity.inf` is implemented as a singleton object. There is only one instance of `inf` and `-inf`. You should not attempt to create new instances or modify its state.
- gotcha While `infinity.inf` handles comparisons broadly, its interaction with `float('nan')` (Not a Number) can be inconsistent due to `NaN`'s unique comparison rules (e.g., `NaN == NaN` is False). Comparing `inf` with `NaN` might not always yield intuitive results.
Install
-
pip install infinity
Imports
- inf
import infinity; my_inf = infinity.Infinity()
from infinity import inf
Quickstart
from infinity import inf
# Universal comparison
print(f"inf > 1000: {inf > 1000}")
print(f"inf < 1000: {inf < 1000}")
print(f"inf == float('inf'): {inf == float('inf')}")
# Compare with non-numeric types (unique feature of infinity.inf)
print(f"inf > 'hello': {inf > 'hello'}")
print(f"inf > []: {inf > []}")
print(f"inf > {'a': 1}: {inf > {'a': 1}}")
# Using negative infinity
neg_inf = -inf
print(f"neg_inf < -1000: {neg_inf < -1000}")