Is that possible to intercept declarates new attributes in python class?

62 Views Asked by At

Creating new attributes in class instace we can intercept with __setattr__ magic function. self.new_attribute = "hi!"

But if I want to intercept creation of class attributes?

class NewClass():
     new_attribute = "hi!"

Is that possible? If so, how? Metaclasses?

P.S. It's possible with using inspect inside the class (such as DeclareAttribute("new_attribute", "hi!") with using getframe inside), but in this case I'm losing possibility of code navigation to this name.

0

There are 0 best solutions below