The Problem
Say I have a class Root and want to access (e.g. initialise) all its subclasses. But there can be some subclasses that need to be ignored programatically.
Example
class Root(object):
pass
class Parent(Root):
ignore_me = True
class Child(Parent):
pass
subs = [sub for sub in Root.__subclasses__() if not sub.ignore_me]
So what I want here is that Child class is included in the subs list as opposed to the Parent class.
Trivial Solution
Of course, I could define the ignore_me attribute for each subclass, but the point is that I want to isolate the subclasses from that detail, so that they will not even be aware of it.
Question
How can I achieve the goal by just defining the ignore_me attribute only in Parent class?
Aucun commentaire:
Enregistrer un commentaire