jangada.mixin.Identifiable#
- class jangada.mixin.Identifiable#
Bases:
objectMixin that adds a globally unique, write-once UUID identifier.
Provides each instance with a unique ID that cannot be changed after initialization. Instances are tracked in a weak-reference registry, allowing lookup by ID while permitting garbage collection.
The ID is a UUID v4 (randomly generated) stored as a 32-character hexadecimal string. It is not included when copying objects (copiable=False).
- Attributes:
- idstr
A unique 32-character hexadecimal UUID v4 identifier. Write-once, auto-generated if not provided, and not copied.
See also
TaggableSymbolic identifier for namespace access
SerializablePropertyProperty descriptor used for id
Notes
- Equality Semantics
Two Identifiable objects are equal if they have the same ID. This implements value equality; for identity equality use
is. Subclasses can override__eq__for content-based equality.- Hash Consistency
Hash is based solely on ID, ensuring objects can be used in sets and as dictionary keys. Hash remains consistent even if other attributes change.
- Weak References
The registry uses weak references, so instances can be garbage collected even while registered. After garbage collection,
get_instance()will return None for that ID.- Copy Behavior
The ID is not copied when using
copy.copy()or serialization withis_copy=True. Each copy gets a new, unique ID.- Thread Safety
The registry is not thread-safe. Concurrent access from multiple threads may require external synchronization.
Examples
Basic usage:
class Component(Identifiable): pass comp = Component() print(comp.id) # '3a5f8e2c1b9d4f7a...'
Lookup by ID:
comp_id = comp.id retrieved = Identifiable.get_instance(comp_id) assert retrieved is comp
Hash and equality based on ID:
comp1 = Component() comp2 = Component() # Different IDs assert comp1 != comp2 assert hash(comp1) != hash(comp2) # Can use in sets and dicts components = {comp1, comp2}
Write-once protection:
comp = Component() original_id = comp.id # Cannot change ID comp.id = 'different-id' # Raises AttributeError
- __init__(*args, **kwargs)#
Methods
__delattr__(name, /)Implement delattr(self, name).
__dir__(/)Default dir() implementation.
__eq__(other)Compare equality based on ID.
__format__(format_spec, /)Default object formatter.
__ge__(value, /)Return self>=value.
__getattribute__(name, /)Return getattr(self, name).
__getstate__(/)Helper for pickle.
__gt__(value, /)Return self>value.
__hash__()Compute hash based on ID.
__init__(*args, **kwargs)__init_subclass__This method is called when a class is subclassed.
__le__(value, /)Return self<=value.
__lt__(value, /)Return self<value.
__ne__(value, /)Return self!=value.
__reduce__(/)Helper for pickle.
__reduce_ex__(protocol, /)Helper for pickle.
__repr__(/)Return repr(self).
__sizeof__(/)Size of object in memory, in bytes.
__str__(/)Return str(self).
__subclasshook__Abstract classes can override this to customize issubclass().
get_instance(id_)Retrieve an Identifiable instance by its ID.
Attributes
__annotations___instancesGlobally unique identifier (UUID v4).