metaclasses

Carl Banks imbosol at aerojockey.com
Wed Jul 30 02:17:54 CEST 2003


Raymond Hettinger wrote:
>> It seems this is a more general way to build classes than inheritance.
>> Is this a reasonable viewpoint?
>>
>> >>>
>> >>> def meth1(self):
>> ...   print "i am meth1"
>> ...
>> >>> def meth2(self):
>> ...   print "i am meth2"
>> ...
>> >>> Foo = type("Foo",(),{'meth':meth1}) # make a class
>> >>> foo = Foo() # make an instance
>> >>> foo.meth() # call a method
>> i am meth1
>  
> It is a less general way for building classes.

Actually, the question doesn't make sense.  Metaclasses and
inheritance are two different dimensions of generality.  Neither is
more or less general than the other, because they generalize in
different ways.

However, his use of the built-in metatype 'type' is more general than
the class statement, which seemed to be what he was asking.  One can
supply a list of bases which, unlike the class statement, need not be
a fixed length.  In fact, the bases can be calculated dynamically.
Likewise with the class dict.


-- 
CARL BANKS




More information about the Python-list mailing list