class MType(type):
pass
class MClass(object):
__metaclass__ = MType
a = MType
From the above snippet is there anyway I can create an instance of MClass given a?
(It’s probably important to note that I won’t be able to simply contruct MClass())
There are two straightforward ways I can think of to find the instances of a metaclass. One is to have the metaclass keep track of a list of its instances as they are created, as follows:
Now
MType.instancesis[<class '__main__.MClass'>].Another way is to use the
inspect module to look through the classes in, say, a given module, and use
isinstanceto check if they are instances ofMType. This isn’t necessarily a crazy thing to do, either – for example, various unit testing suites work by looking through a module for classes and functions that appear to be tests, based on their names and/or inherited classes – I’m not really sure why you would want to do this with a metaclass, though.