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.
pip install constantlyVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your environment uses Python 3.8 or newer. If you need Python 3.7 support, pin `constantly<23.10.0`.
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.
Constants are immutable by design. Define them once and treat them as fixed values throughout your application.
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.
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.
No dependency data recorded yet.