元类示例1 meta1.py

meta1.py:
 
 
from time import ctime

print '*** Welcome to Metaclass'
print'\tMetaclass declaration first.'

class MetaC(type):
    def __init__(cls,name,bases,attrd):
        super(MetaC,cls).__init__(name,bases,attrd)
        print '***Created class %r at: %s' %(name,ctime())

print '\tClass "Foo" declaration next.'

class Foo(object):
    __metaclass__=MetaC
    def __init__(self):
        print '***Instantiated class %r at: %s' %(
            self.__class__.__name__,ctime())

print '\tClass "Foo" instantiation next.'
f=Foo()
print '\tDone'


>>> *** Welcome to Metaclass
	Metaclass declaration first.
	Class "Foo" declaration next.
***Created class 'Foo' at: Mon Dec 17 18:50:19 2012
	Class "Foo" instantiation next.
***Instantiated class 'Foo' at: Mon Dec 17 18:50:19 2012
	Done


你可能感兴趣的:(元类示例1 meta1.py)