I have 2 classes A and B:
class A(object):
x = 0
class B(object):
y = 0
How can I make it so B “inherits” A‘s class-level variables (x in this case) by using decorators? Is it at all possible? The desired behavior (if possible), after decorated, B would look like this:
class B(object):
x = 0
y = 0
Note: If anyone wants/needs to know why I’m asking this, it’s simply to make SQLAlchemy’s Concrete Table Inheritance look nicer in code, although I can see many use cases for such behavior.
Sure you can; you can use a class decorator that takes class A as an argument, then updates the decorated class for you:
The decorator copies anything that is really an attribute (not a function or a property), and doesn’t start with a double underscore.
Usage:
Tested on the prompt: