Metaclass discussons

Discussion in 'Python' started by Gustavo Niemeyer, Aug 22, 2003.

  1. Hi everyone!

    I was just part of a small discussion concerning metaclass features on
    Python, and one limitation that was appointed was the inability to
    redefine metaclasses on existent code. Than I thought it was a good
    way to explore some more the metaclass concepts..

    Basically, people were arguing that there are some uses for that, based
    on plugging new knowledge on existent classes and instances. Without
    complaining too much about usage issues, I started looking for ways to
    dismiss that limitation.

    Without yet considering interpreter hacks, that's what I had as a first
    solution (a very limited one, I must admit):

    class M(type): pass
    __metaclass__ = M
    class object:
    __metaclass__ = M
    execfile("somemodule.py", globals(), locals())

    Can you imagine any alternative which is not based on the same concept
    (executing the module code on a hacked context)?

    --
    Gustavo Niemeyer
    http://niemeyer.net
     
    Gustavo Niemeyer, Aug 22, 2003
    #1
    1. Advertising

  2. Gustavo Niemeyer

    David Mertz Guest

    Gustavo Niemeyer <> wrote previously:
    |Can you imagine any alternative which is not based on the same concept
    |(executing the module code on a hacked context)?

    In Gnosis Utilities, the function gnosis.magic.import_with_metaclass()
    does basically the same thing, but a bit more neatly packaged up. You
    might give that a try for imposing metaclasses on older code.

    Michele Simionato has proposed some enhancements to resolve issues of
    metatype conflicts, but I haven't quite decided to bundle them up in
    Gnosis, since that add quite a bit of complication that is not needed
    95% of the time. But one day, I suppose I will. However, he has posted
    the same general concepts to ActiveState's Python Cookbook; and the
    article by my Michele and I that discusses some of this should FINALLY
    appear at IBM developerWorks this week (we wrote it quite some months
    ago; and it's been available on my website for some of the interim).

    Yours, David...

    --
    Keeping medicines from the bloodstreams of the sick; food from the bellies
    of the hungry; books from the hands of the uneducated; technology from the
    underdeveloped; and putting advocates of freedom in prisons. Intellectual
    property is to the 21st century what the slave trade was to the 16th.
    --
    X-Shameless-Plug: Buy Text Processing in Python: http://tinyurl.com/jskh
     
    David Mertz, Aug 24, 2003
    #2
    1. Advertising

  3. Gustavo Niemeyer <> wrote in message news:<>...
    > Hi everyone!
    >
    > I was just part of a small discussion concerning metaclass features on
    > Python, and one limitation that was appointed was the inability to
    > redefine metaclasses on existent code. Than I thought it was a good
    > way to explore some more the metaclass concepts..
    >
    > Basically, people were arguing that there are some uses for that, based
    > on plugging new knowledge on existent classes and instances. Without
    > complaining too much about usage issues, I started looking for ways to
    > dismiss that limitation.
    >
    > Without yet considering interpreter hacks, that's what I had as a first
    > solution (a very limited one, I must admit):
    >
    > class M(type): pass
    > __metaclass__ = M
    > class object:
    > __metaclass__ = M
    > execfile("somemodule.py", globals(), locals())
    >
    > Can you imagine any alternative which is not based on the same concept
    > (executing the module code on a hacked context)?


    Oh, what a wonderfully evil hack! Thanks Gustavo, I never come to my
    mind that I could redefine the object class!
    I was using the hack of modifing the source code by adding a __metaclass__
    atribute and re-exec-uting it, but I didn't like the trick. Then I wrote
    a script to regenerate an entire hierarchy with a different metaclass.
    Much less hackish, but a bit non-trivial. Your idea is perfect for easy
    hacks in the debugging phase (i.e. add a metaclass generating logging
    capabilities or other more general debugging utilities). You can
    spell it even as

    class object(object):
    class __metaclass__(type):
    pass

    # rest of the code

    Michele Simionato, Ph. D.

    http://www.phyast.pitt.edu/~micheles
    --- Currently looking for a job ---
     
    Michele Simionato, Aug 24, 2003
    #3
  4. On 24 Aug 2003 04:46:26 -0700
    (Michele Simionato) wrote:

    > Much less hackish, but a bit non-trivial. Your idea is perfect for
    > easy hacks in the debugging phase (i.e. add a metaclass generating
    > logging capabilities or other more general debugging utilities). You
    > can spell it even as
    >
    > class object(object):
    > class __metaclass__(type):
    > pass
    >
    > # rest of the code


    Humm... but it does't propagate to imported modules

    # mod.py

    class Klass(object): pass

    # end

    ---

    >>> class object(object):

    .... class __metaclass__(type): pass
    ....
    >>> import mod
    >>> mod.Klass.__bases__

    (<type 'object'>,)
    >>> class A(object): pass

    ....
    >>> A.__bases__

    (<class '__main__.object'>,)


    --

    But assigning it to __builtin__.object do:

    >>> class metaclass(type): pass

    ....
    >>> import __builtin__
    >>> __builtin__.object = metaclass("object", (), {})
    >>> import mod
    >>> mod.Klass.__bases__

    (<class '__main__.object'>,)
    >>> class A(object): pass

    ....
    >>> A.__bases__

    (<class '__main__.object'>,)
    >>>



    Rgds

    Pedro
     
    Pedro Werneck, Aug 24, 2003
    #4
  5. On 24 Aug 2003 04:46:26 -0700
    (Michele Simionato) wrote:

    > Much less hackish, but a bit non-trivial. Your idea is perfect for
    > easy hacks in the debugging phase (i.e. add a metaclass generating
    > logging capabilities or other more general debugging utilities). You
    > can spell it even as
    >
    > class object(object):
    > class __metaclass__(type):
    > pass
    >
    > # rest of the code


    Humm... but it does't propagate to imported modules

    # mod.py

    class Klass(object): pass

    # end

    ---

    >>> class object(object):

    .... class __metaclass__(type): pass
    ....
    >>> import mod
    >>> mod.Klass.__bases__

    (<type 'object'>,)
    >>> class A(object): pass

    ....
    >>> A.__bases__

    (<class '__main__.object'>,)


    --

    But assigning it to __builtin__.object do:

    >>> class metaclass(type): pass

    ....
    >>> import __builtin__
    >>> __builtin__.object = metaclass("object", (), {})
    >>> import mod
    >>> mod.Klass.__bases__

    (<class '__main__.object'>,)
    >>> class A(object): pass

    ....
    >>> A.__bases__

    (<class '__main__.object'>,)
    >>>



    Rgds

    Pedro
     
    Pedro Werneck, Aug 24, 2003
    #5
  6. Hello Michele!

    > class object(object):
    > class __metaclass__(type):
    > pass


    Thanks for the hint! After sending I found out that it should have
    the real object as parent. OTOH, your nested definition is really
    elegant! :)

    --
    Gustavo Niemeyer
    http://niemeyer.net
     
    Gustavo Niemeyer, Aug 25, 2003
    #6
    1. Advertising

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. Replies:
    9
    Views:
    369
    David Mertz
    Sep 10, 2003
  2. Metaclass and __slots__

    , Nov 6, 2003, in forum: Python
    Replies:
    1
    Views:
    316
    Peter Otten
    Nov 6, 2003
  3. Jp Calderone
    Replies:
    2
    Views:
    317
    Alex Martelli
    Nov 10, 2003
  4. ironfroggy
    Replies:
    16
    Views:
    442
    Michele Simionato
    Jun 3, 2005
  5. Steven D'Aprano

    Metaclass of a metaclass

    Steven D'Aprano, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    310
Loading...

Share This Page