Re: metaclasses: timestamping instances

Discussion in 'Python' started by Steve Holden, Sep 1, 2007.

  1. Steve Holden

    Steve Holden Guest

    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 -------------
    Steve Holden, Sep 1, 2007
    1. Advertisements

  2. On Sep 1, 6:07 pm, Steve Holden <> 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
    Michele Simionato, Sep 1, 2007
    1. Advertisements

  3. En Mon, 03 Sep 2007 07:39:05 -0300, km <>

    > 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
    Gabriel Genellina, Sep 4, 2007
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wohlbier
    Josiah Carlson
    Feb 22, 2004
  2. Replies:
    James Stroud
    Jan 29, 2009
  3. RonnBlack
    Jun 18, 2008
  4. Roedy Green

    timestamping Jars

    Roedy Green, Jan 21, 2014, in forum: Java
    Roedy Green
    Jan 21, 2014
  5. Roedy Green
    Roedy Green
    Apr 11, 2014

Share This Page