class Test:
def __init__(self, *args, **kwargs):
self._meta = {}
def __getattr__(self, name):
if name in self._meta:
return self._meta[name]
return object.__getattr__(self, name)
def __setattr__(self, name, value):
if name != '_meta':
self._meta[name] = value
super().__setattr__(name, value)
@property
def fields(self):
return [x.to_representation() for x in self._meta.values()]
f1 = Field('foo', 123, True)
f2 = Field('bar', 4321, False)
t = Test()
t.f1 = f1
t.f2 = f2
print(t._meta) # {'f1': <__main__.Field object at 0x101faa208>, 'f2': <__main__.Field object at 0x101faa390>}
print(t.f1) # <__main__.Field object at 0x101faa208>
print(t.f2) # <__main__.Field object at 0x101faa390>
print(t.fields) # [{'label': 'foo', 'value': 123, 'flag': True}, {'label': 'bar', 'value': 4321, 'flag': False}]
class Test:
def __init__(self, *args, **kwargs):
self.fields = []
тогда никак