I want to do something similar, but so far I have not had much success. I would like to make each attr a property that _lazy_eval only evaluates on access:
class Base(object): def __init__(self): for attr in self._myattrs: setattr(self, attr, property(lambda self: self._lazy_eval(attr))) def _lazy_eval(self, attr):
** UPDATE **
This also does not work:
class Base(object): def __new__(cls): for attr in cls._myattrs: setattr(cls, attr, property(lambda self: self._lazy_eval(attr))) return object.__new__(cls)
** UPDATE 2 **
Used the __metaclass__ solution, but instead inserted it into Base.__new__ . It seems that to correctly define the property, it was necessary to define a clearer closure - "prop ()":
class Base(object): def __new__(cls): def prop(x): return property(lambda self: self._lazy_eval(x)) for attr in cls._myattrs: setattr(cls, attr, prop(attr)) return object.__new__(cls)
user297250
source share