{"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":"python","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":[{"wrong":"from constantly import Constant","symbol":"FlagConstant","correct":"from constantly import FlagConstant"},{"wrong":"from constantly import Constant","symbol":"Flags","correct":"from constantly import Flags"},{"wrong":"from constantly import Constant","symbol":"NamedConstant","correct":"from constantly import NamedConstant"}],"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"},{"fix":"Ensure all `NamedConstant` and `Constant` instances are defined *within* a `Values` class or properly assigned to their `_container` if created dynamically. Misconfiguration or incorrect usage of the library's `Values` class can result in unassigned containers. For example, if defining constants dynamically, explicitly set the `_container` attribute to the `Values` instance.","message":"Accessing a `NamedConstant` or `Constant` instance that was not properly registered with its `Values` container can lead to an `AttributeError: 'NoneType' object has no attribute '__name__'` when its `__repr__` method is called. This typically happens if the constant's `_container` attribute is `None`.","severity":"breaking","affected_versions":"all"},{"fix":"Ensure all `NamedConstant` instances are defined as class attributes within a `constantly.Values` subclass. This allows the `ValuesMetaClass` to correctly set the `_container` attribute for each constant. If you instantiate `NamedConstant` directly, its default `__repr__` will fail unless `_container` is manually set or a custom `__repr__` is provided.","message":"Attempting to represent (e.g., `print` or `repr()`) a `NamedConstant` instance whose internal `_container` attribute is `None` will result in an `AttributeError: 'NoneType' object has no attribute '__name__'`.","severity":"breaking","affected_versions":"all"}],"env_vars":null,"search_vec":"'23.10.4':42 'appear':36 'compar':24 'constant':1,3,4,11,43 'creat':15 'current':39 'develop':47 'easili':23 'enum':44 'enumer':16 'ident':26 'immut':19,46 'librari':8 'maintain':29 'particular':12 'period':37 'project':33 'provid':9 'python':7 'releas':35 'symbol':2,10,45 'twist':32 'type':17 'use':13 'valu':20 'version':40","created_at":"2026-04-09T03:47:36.658852+00:00","updated_at":"2026-04-16T14:23:45.581137+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":0,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"23.10.4","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/twisted/constantly","docs":null,"changelog":null,"pypi":"https://pypi.org/project/constantly/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["type-stubs"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-30","last_verified":"2026-08-27","next_check":"2026-07-30","install_tag":"stale"}}