2👍
This could be done with a decorator:
from SocialProvider import SocialProvider
@SocialProvider.Register
class NewSocialProvider( SocialProvider ):
pass
I’ve assumed SocialProvider
is a superclass written by you. If so, in your superclass file, the Register
helper could be defined as a class method:
class SocialProvider:
subclasses = []
@classmethod
def Register( cls, subcl ):
cls.subclasses.append( subcl )
return subcl
If not, then you could still define a function somewhere, to be used as a decorator, but it and the list it appends to would have to be defined somewhere else, separate from the SocialProvider
class. You mention the problem of not-yet-imported files. There’s nothing you can do to detect a class that hasn’t been defined yet, so you’d have to make sure that the files with the subclass definitions are imported (e.g. via statements like import facebook
etc in the ___init__.py
file of your module).
👤jez
Source:stackexchange.com