0

So I want to add class attributes to my python classes (which may depend on other class attributes such as cls.__name__, etc. at definition time). I am aware of two ways of doing this:

  1. Write a meta-class. I don't like this because (1) why should I have to write 2 classes each time I write a class (2) it has other disadvantages like the attributes in the meta-class nor showing up when querying dir(MyClass)

  2. Use @classmethod + @property which is possible since python 3.9. This is what I am using atm, but to be fair, the experience has been horrible so far. There are crazy bugs and using this combination of decorators is currently incompatible with important development tools like mypy.

There must be a better way!

Is maybe a custom @classattribute descriptor the solution?

Hyperplane
  • 964
  • 1
  • 8
  • 25

0 Answers0