I want to bind a dynamically created namedtuple
to a class
When an instance of the class is initialized, a new
namedtuple
-type is created, and registered inglobals()
(this allows pickling).When the instance is deleted, I want to perform cleanup of the registration in
globals()
.
I already have working code for this:
from collections import namedtuple
class ClassWithTuple:
tuple_id: str
tuple_type: type[tuple]
def __init__(self, name: str, fields: list[str]) -> None:
self.tuple_type = namedtuple(name, fields)
self.tuple_id = f"_{name}_{self.__class__.__name__}_{hash(self)}"
self.tuple_type.__qualname__ = self.tuple_id
if self.tuple_id in globals():
raise RuntimeError(f"A class '{self.tuple_id}' exists!")
globals()[self.tuple_id] = self.tuple_id
def __del__(self):
del globals()[self.tuple_id]
del self
obj = ClassWithTuple("FooTuple", ["a", "b", "c"])
obj.tuple_type(1, 2, 3)
What I'd like to do is refactor this in the form
class ClassWithTuple
tuple_id: str
tuple_type: type[tuple]
def __init__(self, name: str, fields: list[str]) -> None:
register_tuple(self, name, fields)
or possibly self.tuple_id, self.tuple_type = register_tuple(self, name, fields)
.
How can I automagically add the required __del__
cleanup code? (Especially, if the class defined __del__
, then del globals()[self.tuple_id]
should be prepended I suppose.)