Hi. First post, be gentle!
I’d like to be able to find all current subclasses of a given class. The obvious answer is
__subclasses__, but that feature is not kept up to date in the face of class redefinition and scoping. For example:
class Foo: pass class Bar(Foo): pass class Bar: pass
At this point,
Foo.__subclasses__() is still a list of length 1, containing
def fn1(): class Baz(Foo): pass fn1() fn1() fn1()
Foo.__subclasses__() has length 3, with three copies of a local class
Presumably it’s too difficult to keep
__subclasses__ up to date, otherwise I assume it would be done. But as things stand it changes only at garbage collection; if I append
gc.collect() to either of the examples above,
Foo.__subclasses__() then returns an empty list.
My question is, what might I do? Is there another mechanism I can use? Is it worth asking the Python Gods whether
__subclasses__() can be made accurate? Should I redefine the function myself to call gc each time, just to be certain? Any ideas appreciated.