metaclasses: timestamping instances

km srikrishnamohan at
Sat Sep 1 16:06:55 CEST 2007

Hi all,

I have extended a prototype idea from Alex Martelli's resource on
metaclasses regarding time stamping of instances.

import time
class Meta(type):
    start = time.time()
    def __call__(cls, *args, **kw):
        print 'Meta start time  %e'%cls.start
        x = super(Meta, cls).__call__(*args, **kw)
        current_time = time.time()
        x._created = current_time - Meta.start
        Meta.start = time.time()
        return x

class X(object):
    __metaclass__ = Meta
class Y(X):
    __metaclass__ = Meta
a = X()
print 'a time stamp %e'%a._created
b = Y()
print 'b time stamp %e'%b._created
print abs(a._created - b._created)

I donot understand the difference between
1) setting __metaclass__ to 'Meta'  in class Y
2) not setting  __metaclass__  to Meta in class Y

Why is the difference in behaviour of time stamping between 1 & 2 ?

kindly enlighten
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list