metaclasses: timestamping instances

km srikrishnamohan at gmail.com
Sat Sep 1 10:06:55 EDT 2007


Hi all,

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

<code>
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
    pass
a = X()
print 'a time stamp %e'%a._created
b = Y()
print 'b time stamp %e'%b._created
print abs(a._created - b._created)
</code>

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
regards,
KM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070901/2cbded40/attachment.html>


More information about the Python-list mailing list