metaclasses

Discussion in 'Python' started by castironpi@gmail.com, Mar 4, 2008.

  1. Guest

    What are metaclasses?
     
    , Mar 4, 2008
    #1
    1. Advertising

  2. Daniel Fetchinson, Mar 4, 2008
    #2
    1. Advertising

  3. Guest

    , Mar 4, 2008
    #3
  4. Benjamin Guest

    On Mar 3, 7:12 pm, wrote:
    > What are metaclasses?


    Depends on whether you want to be confused or not. If you do, look at
    this old but still head bursting essay: http://www.python.org/doc/essays/metaclasses/.

    Basically, the metaclass of a (new-style) class is responsible for
    creating the class. This means when Python sees
    class Foo(object):
    __metaclass__ = FooMeta
    class FooMeta(type):
    def __new__(cls, name, bases, dct):
    #do something cool to the class
    pass
    It asks FooMeta to create the class Foo. Metaclasses always extend
    type because type is the default metaclass.
     
    Benjamin, Mar 4, 2008
    #4
  5. Guest

    On Mar 3, 10:01 pm, Benjamin <> wrote:
    > On Mar 3, 7:12 pm, wrote:
    >
    > > What are metaclasses?

    >
    > Depends on whether you want to be confused or not. If you do, look at
    > this old but still head bursting essay:http://www.python.org/doc/essays/metaclasses/.
    >
    > Basically, the metaclass of a (new-style) class is responsible for
    > creating the class. This means when Python sees
    > class Foo(object):
    >     __metaclass__ = FooMeta
    > class FooMeta(type):
    >     def __new__(cls, name, bases, dct):
    >        #do something cool to the class
    >        pass
    > It asks FooMeta to create the class Foo. Metaclasses always extend
    > type because type is the default metaclass.


    But you can stack class decorations, but not class metas.

    @somethingcool1
    @somethingcool2
    class Foo:
    pass

    * class Foo:
    __metaclass__= MetaCool1, MetaCool

    * denotes malformed
     
    , Mar 4, 2008
    #5
  6. On Mar 4, 6:31 am, wrote:
    > On Mar 3, 10:01 pm, Benjamin <> wrote:
    >
    >
    >
    > > On Mar 3, 7:12 pm, wrote:

    >
    > > > What are metaclasses?

    >
    > > Depends on whether you want to be confused or not. If you do, look at
    > > this old but still head bursting essay:http://www.python.org/doc/essays/metaclasses/.

    >
    > > Basically, the metaclass of a (new-style) class is responsible for
    > > creating the class. This means when Python sees
    > > class Foo(object):
    > > __metaclass__ = FooMeta
    > > class FooMeta(type):
    > > def __new__(cls, name, bases, dct):
    > > #do something cool to the class
    > > pass
    > > It asks FooMeta to create the class Foo. Metaclasses always extend
    > > type because type is the default metaclass.

    >
    > But you can stack class decorations, but not class metas.
    >
    > @somethingcool1
    > @somethingcool2
    > class Foo:
    > pass
    >
    > * class Foo:
    > __metaclass__= MetaCool1, MetaCool
    >
    > * denotes malformed


    -----------------------
    class Meta1(type):

    def foo1(cls):
    print 'hello'

    class Meta2(type):

    def foo2(cls):
    print 'castiron'

    class Meta(Meta1, Meta2):
    pass

    class Base(object):
    __metaclass__ = Meta


    Base.foo1()
    Base.foo2()

    -----------------------

    Gerard
     
    Gerard Flanagan, Mar 4, 2008
    #6
  7. Guest

    On Mar 4, 12:51 am, Gerard Flanagan <> wrote:
    > On Mar 4, 6:31 am, wrote:
    >
    >
    >
    >
    >
    > > On Mar 3, 10:01 pm, Benjamin <> wrote:

    >
    > > > On Mar 3, 7:12 pm, wrote:

    >
    > > > > What are metaclasses?

    >
    > > > Depends on whether you want to be confused or not. If you do, look at
    > > > this old but still head bursting essay:http://www.python.org/doc/essays/metaclasses/.

    >
    > > > Basically, the metaclass of a (new-style) class is responsible for
    > > > creating the class. This means when Python sees
    > > > class Foo(object):
    > > >     __metaclass__ = FooMeta
    > > > class FooMeta(type):
    > > >     def __new__(cls, name, bases, dct):
    > > >        #do something cool to the class
    > > >        pass
    > > > It asks FooMeta to create the class Foo. Metaclasses always extend
    > > > type because type is the default metaclass.

    >
    > > But you can stack class decorations, but not class metas.

    >
    > > @somethingcool1
    > > @somethingcool2
    > > class Foo:
    > >    pass

    >
    > > * class Foo:
    > >    __metaclass__= MetaCool1, MetaCool

    >
    > > * denotes malformed

    >
    > -----------------------
    > class Meta1(type):
    >
    >     def foo1(cls):
    >         print 'hello'
    >
    > class Meta2(type):
    >
    >     def foo2(cls):
    >         print 'castiron'
    >
    > class Meta(Meta1, Meta2):
    >     pass
    >
    > class Base(object):
    >     __metaclass__ = Meta
    >
    > Base.foo1()
    > Base.foo2()


    class Base(object):
    __metaclass__= type( 'Meta', ( Meta1, Meta2 ), {} )
     
    , Mar 4, 2008
    #7
    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. Stephan Diehl

    question about metaclasses

    Stephan Diehl, Jun 30, 2003, in forum: Python
    Replies:
    2
    Views:
    335
    Stephan Diehl
    Jun 30, 2003
  2. Simon Burton

    metaclasses

    Simon Burton, Jul 28, 2003, in forum: Python
    Replies:
    5
    Views:
    381
    Simon Burton
    Jul 30, 2003
  3. Mike C. Fletcher

    Metaclasses presentation slides available...

    Mike C. Fletcher, Aug 28, 2003, in forum: Python
    Replies:
    3
    Views:
    336
    Alex Martelli
    Aug 29, 2003
  4. Simon Burton

    metaclasses for type comparison

    Simon Burton, Sep 11, 2003, in forum: Python
    Replies:
    5
    Views:
    298
    David Eppstein
    Sep 11, 2003
  5. Santiago Aguiar

    AOP and metaclasses

    Santiago Aguiar, Feb 20, 2004, in forum: Python
    Replies:
    2
    Views:
    325
    John Roth
    Feb 20, 2004
Loading...

Share This Page