{"id":1430,"library":"constantly","title":"Constantly: Symbolic Constants","description":"constantly is a Python library providing symbolic constants, particularly useful for creating enumerated types and immutable values that are easily comparable by identity. It is maintained by the Twisted project, with releases appearing periodically. The current version is 23.10.4.","status":"active","version":"23.10.4","language":"en","source_language":"en","source_url":"https://github.com/twisted/constantly","tags":["constants","enum","symbolic","immutability","development"],"install":[{"cmd":"pip install constantly","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"For simple, standalone symbolic constants.","symbol":"Constant","correct":"from constantly import Constant"},{"note":"Used as attributes within a Values class for enumerated types.","symbol":"NamedConstant","correct":"from constantly import NamedConstant"},{"note":"A base class to group NamedConstants into an enumerable type.","symbol":"Values","correct":"from constantly import Values"}],"quickstart":{"code":"from constantly import NamedConstant, Values\n\nclass Fruit(Values):\n    apple = NamedConstant()\n    banana = NamedConstant()\n    orange = NamedConstant()\n\n# Accessing constants\nprint(f\"Apple constant: {Fruit.apple}\")\nprint(f\"Apple name: {Fruit.apple.name}\")\nprint(f\"Apple value: {Fruit.apple.value}\")\n\n# Comparing constants by identity (preferred)\nprint(f\"Is Fruit.apple the same object as Fruit.apple? {Fruit.apple is Fruit.apple}\")\nprint(f\"Is Fruit.apple the same object as Fruit.banana? {Fruit.apple is Fruit.banana}\")\n\n# Iterating over constants in the class\nprint(\"All fruits:\")\nfor fruit in Fruit.iterconstants():\n    print(f\"- {fruit.name}\")","lang":"python","description":"This example demonstrates how to define a set of named constants using `Values` and `NamedConstant`, and how to access and compare them. `NamedConstant` instances within a `Values` class are singletons."},"warnings":[{"fix":"Ensure your environment uses Python 3.8 or newer. If you need Python 3.7 support, pin `constantly<23.10.0`.","message":"Python 3.7 support was dropped in `constantly` version 23.10.0.","severity":"breaking","affected_versions":">=23.10.0"},{"fix":"Always use `constant_a is constant_b` for comparing two `NamedConstant` instances. While `==` works for comparing a `NamedConstant` to its string name, `is` ensures you're comparing the exact symbolic object.","message":"NamedConstants defined within a `Values` class are singletons. Prefer identity comparison (`is`) over value comparison (`==`) for correctness, especially when comparing against other objects or similar strings.","severity":"gotcha","affected_versions":"all"},{"fix":"Constants are immutable by design. Define them once and treat them as fixed values throughout your application.","message":"Attempting to modify the `name` or `value` attributes of a `Constant` or `NamedConstant` after definition will result in an `AttributeError`, as they are designed to be immutable.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}