I am new to metaclasses, and may be using them in unintended ways. I'm puzzled by the fact that the isinstance() method appears to have transitive behavior when dealing with subclasses, but not when dealing with metaclasses.
In the first case:
class A(object):
pass
class B(A):
pass
class C(B):
pass
ic = C()
implies that isinstance(ic,X) is true for X equal to A, B or C.
On the other hand, here is a metaclass example:
class DataElementBase(object):
def __init__(self,value):
self.value = self.__initialisation_function__(value)
class MetaDataElement(type):
def __new__(cls,name,initialisation_function, helptext ):
result = type.__new__(cls,name,(DataElementBase,), dict(help=helptext) )
result.__initialisation_function__ = staticmethod(initialisation_function)
return result
### test code ####
# create a class from the metaclass
MyClass = MetaDataElement( 'myclass', float, "Value is obtained as float of user input" )
# create an instance of the class
my_instance = MyClass( '4.55' )
print ( 'MyClass is instance of MetaDataElement? %s' % isinstance( MyClass, MetaDataElement ) )
print ( 'MyClass is instance of DataElementBase? %s' % isinstance( MyClass, DataElementBase ) )
print ( 'my_instance is instance of MyClass? %s' % isinstance( my_instance, MyClass ) )
print ( 'my_instance is instance of MetaDataElement? %s' % isinstance( my_instance, MetaDataElement ) )
print ( 'my_instance is instance of DataElementBase? %s' % isinstance( my_instance, DataElementBase ) )
yields:
MyClass is instance of MetaDataElement? True
MyClass is instance of DataElementBase? False
my_instance is instance of MyClass? True
my_instance is instance of MetaDataElement? False
my_instance is instance of DataElementBase? True
That, is MyClass is an instance of the MetaDataElement metaclass, and my_instance is an instance of the MyClass class but not of MetaDataElement.
Is my interpretation correct? Is there a simple explanation for this?