Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: metaclasses: timestamping instances

Thread Tools

Re: metaclasses: timestamping instances

Steve Holden
Posts: n/a
km wrote:
> 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

I don't see and difference. The rules for establishing the metaclass of
a class are fairly well defined: see

which says

* If dict['__metaclass__'] exists, it is used.

* Otherwise, if there is at least one base class, its metaclass is
used (this looks for a __class__ attribute first and if not found, uses
its type).

* Otherwise, if a global variable named __metaclass__ exists, it is

* Otherwise, the old-style, classic metaclass (types.ClassType) is

By the second rule it would appear that the metaclass of X is the same
as that of Y even if the __metaclass__ assignment is commented out, and
informal testing appears to show that this is the case.

Debugging with Wing IDE and examining the classes at a breakpoint shows
this to be true (even after Y's __metaclass__ assignment is commented out):

>>> X.__metaclass__

<class '__main__.Meta'>
>>> Y.__metaclass__

<class '__main__.Meta'>

Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd
Skype: holdenweb
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Reply With Quote
Michele Simionato
Posts: n/a
On Sep 1, 6:07 pm, Steve Holden <(E-Mail Removed)> wrote:
> Debugging with Wing IDE and examining the classes at a breakpoint shows
> this to be true (even after Y's __metaclass__ assignment is commented out):
> >>> X.__metaclass__

> <class '__main__.Meta'>
> >>> Y.__metaclass__

> <class '__main__.Meta'>
> >>>

For the benefit of the readers I will just point out that in order
to determine the metaclass of a class it is far better NOT to relay on
__metaclass__ attribute. The right thing to to is to look at the
attribute, or to use type. Here is a (made up) example
where .__metaclass__ gives
the wrong result:

In [9]: class M(type): pass

In [10]: class B: __metaclass__ = M

In [11]: B.__metaclass__ = None # now the hook is set to None, but the
metaclass does not change

In [12]: B.__class__
Out[12]: <class '__main__.M'>

Michele Simionato

Reply With Quote
Gabriel Genellina
Posts: n/a
En Mon, 03 Sep 2007 07:39:05 -0300, km <(E-Mail Removed)>

> But why does it show varied difference in the time between a and b
> instance creations when __metaclass__ hook is used and when not used in
> class Y ? I dont understand that point !

What do you expect from a._created, b._created, and its difference?
You reset Meta.start each time - so _created measures the time between
creation of two consecutive instances (minus a small delay because of the
*two* calls to time.time())
And obviously the elapsed time until the second instance is created is
larger, due to the intervening print statement.

Gabriel Genellina

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reusing website SSL keys for digital timestamping? Bennu Strelitzia Computer Security 0 01-09-2010 06:10 PM
dicts,instances,containers, slotted instances, et cetera. Python 8 01-29-2009 09:52 AM
Impact of Verisign Timestamping Service on ClickOnce deployed Apps RonnBlack ASP .Net Security 0 06-18-2008 09:19 PM
Possible to open multiple instances/profiles of Firefox (win32)? Isak Dinesen Firefox 2 07-27-2004 03:12 AM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM