Current Status
I have an abstract base class that, which hosts data in the form of a numpy array, knows how to work this data, and which can explain matplotlib how to draw it. To accomodate different types of data, it has a number of subclasses, like this:
class PlotData():
"""Base Class"""
subclasslist = []
@classmethod
def register(cls):
super().subclasslist.append(cls)
def __new__(self, initdata, *args, **kwargs):
for subclass in subclasslist:
try:
subclass.__test__(initdata)
except AssertionError:
continue
else:
break
else:
raise TypeError("Initdata does not fit any known subclass")
return subclass(initdata, *args, **kwargs)
class Plot3D(PlotData):
"""Subclass for 3d-plotting data"""
def __test__(initdata):
assert Data_is_the_right_kind
class Plot_XY(PlotData):
"""Subclass for for plotting X-Y relations of data"""
def __test__(initdata):
assert Data_is_the_right_kind
The Issue
now, the issue is how to get the class references into the subclasslist. At first I wanted to call super().register() in the class body, but im unable to get a reference to the class itself, which is what I want to store in the list. A small search has yielded two possible solutions, and I was wondering what the best one was.
Solution 1
Adding a call after each class definition, like this:
class Plot_XY(PlotData):
"""Subclass for for plotting X-Y relations of data"""
def __test__(initdata):
assert Data_is_the_right_kind
Plot_XY.register()
This works, but seems like a very dirty solution to me - a very important part of the class structure is located outside of the body.
Solution 2
Another possibility could be class decorators. However, I've never used them before, and the examples I've found are generally used to override/add functionality to methods. (here and here, for example). I am familiar with function decorators though, and the following should roughly make clear what I'm aiming for (and a dumbed down version works in the interpreter):
def some_creative_decorator_name(cls):
cls.register()
return cls
or at least, something that functions like Solution 1 but looks like:
@some_creative_decorator_name
class Plot_XY(PlotData):
"""Subclass for for plotting X-Y relations of data"""
def __test__(initdata):
assert Data_is_the_right_kind
It seems to work just as well, but will this screw up stuff like inheritance? That was one of the concerns noted in the linked pages, and I don't really dare count to much on it. (I am not expecting people to subclass it further, but I don't really want to make it impossible if it's desired.)
(Of course other solutions are welcome as well.)