The original code looks good (although I would not name the local variable by the same name as the closing function).
Please note: properties only work in new-style classes, so you need to inherit the object. In addition, you need to call the property attribute from the instance.
If you need a class attribute, the property will not work, and you will need to write your own descriptor for the class property:
class ClassProperty(object): def __init__(self, func): self.func = func def __get__(self, inst, cls): return self.func(cls) class A(object): model_fields = ['field1', 'field2', 'field3'] @ClassProperty def fields(cls): return cls.model_fields + ['extra_field'] print A.fields
source share