{"id":9830,"library":"infinity","title":"Python Universal Infinity Value","description":"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.","status":"active","version":"1.5","language":"en","source_language":"en","source_url":"https://github.com/kvesteri/infinity","tags":["infinity","comparison","utility","numeric","singleton"],"install":[{"cmd":"pip install infinity","lang":"bash","label":"Install infinity"}],"dependencies":[],"imports":[{"note":"The 'inf' object is a singleton and should be imported directly, not instantiated.","wrong":"import infinity; my_inf = infinity.Infinity()","symbol":"inf","correct":"from infinity import inf"}],"quickstart":{"code":"from infinity import inf\n\n# Universal comparison\nprint(f\"inf > 1000: {inf > 1000}\")\nprint(f\"inf < 1000: {inf < 1000}\")\nprint(f\"inf == float('inf'): {inf == float('inf')}\")\n\n# Compare with non-numeric types (unique feature of infinity.inf)\nprint(f\"inf > 'hello': {inf > 'hello'}\")\nprint(f\"inf > []: {inf > []}\")\nprint(f\"inf > {'a': 1}: {inf > {'a': 1}}\")\n\n# Using negative infinity\nneg_inf = -inf\nprint(f\"neg_inf < -1000: {neg_inf < -1000}\")","lang":"python","description":"Demonstrates importing `inf` and its universal comparison capabilities with various data types, including numbers, strings, and collections, as well as accessing negative infinity."},"warnings":[{"fix":"Use `from infinity import inf` when you need to compare against arbitrary Python objects. Use `float('inf')` or `math.inf` for strictly numerical infinity within mathematical contexts.","message":"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.","severity":"gotcha","affected_versions":"All versions (1.x)"},{"fix":"Always import `inf` directly via `from infinity import inf` and use it as is. Do not try to call `infinity.Infinity()` or similar constructs.","message":"`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.","severity":"gotcha","affected_versions":"All versions (1.x)"},{"fix":"If your data might contain `NaN` values, explicitly check for `math.isnan()` before performing comparisons with `infinity.inf` to ensure your logic handles `NaN` as intended.","message":"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.","severity":"gotcha","affected_versions":"All versions (1.x)"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Add `from infinity import inf` at the top of your Python script or module where you intend to use `inf`.","cause":"The `inf` object was not correctly imported from the `infinity` library.","error":"NameError: name 'inf' is not defined"},{"fix":"The `inf` object is a singleton and should be imported and used directly: `from infinity import inf; my_val = inf`.","cause":"Attempting to instantiate a class named `Infinity` (e.g., `infinity.Infinity()`) instead of using the pre-defined singleton `inf`.","error":"AttributeError: module 'infinity' has no attribute 'Infinity'"},{"fix":"Switch to `infinity.inf` for universal comparisons across different data types. Ensure you have `from infinity import inf` and use `inf` for these broader comparisons.","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.","error":"TypeError: '<' not supported between instances of 'float' and 'str'"}]}