[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

metaclasses

Aaron Brady

3/4/2008 1:13:00 AM

What are metaclasses?
6 Answers

Daniel Fetchinson

3/4/2008 2:22:00 AM

0

> What are metaclasses?

http://www.google.com/search?q=python...

HTH,
Daniel

Aaron Brady

3/4/2008 3:14:00 AM

0

On Mar 3, 8:22 pm, "Daniel Fetchinson" <fetchin...@googlemail.com>
wrote:
> > What are metaclasses?
>
> http://www.google.com/search?q=python...
>
> HTH,
> Daniel

Not satisfied.

http://en.wikipedia.org/wiki/Metaclass#Pyth...

That's a limitation. The constructor can omit the superclass call,
but it can't have keyword arguments, and can't use
AttributeInitType.__call__ with positionals. Subclass
AttributeInition, decorate __init__, or decorate class.

Benjamin

3/4/2008 4:02:00 AM

0

On Mar 3, 7:12 pm, castiro...@gmail.com 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/me....

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.

Aaron Brady

3/4/2008 5:31:00 AM

0

On Mar 3, 10:01 pm, Benjamin <musiccomposit...@gmail.com> wrote:
> On Mar 3, 7:12 pm, castiro...@gmail.com 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/me....
>
> 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

Gerard Flanagan

3/4/2008 6:52:00 AM

0

On Mar 4, 6:31 am, castiro...@gmail.com wrote:
> On Mar 3, 10:01 pm, Benjamin <musiccomposit...@gmail.com> wrote:
>
>
>
> > On Mar 3, 7:12 pm, castiro...@gmail.com 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/me....
>
> > 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

Aaron Brady

3/4/2008 6:17:00 PM

0

On Mar 4, 12:51 am, Gerard Flanagan <grflana...@gmail.com> wrote:
> On Mar 4, 6:31 am, castiro...@gmail.com wrote:
>
>
>
>
>
> > On Mar 3, 10:01 pm, Benjamin <musiccomposit...@gmail.com> wrote:
>
> > > On Mar 3, 7:12 pm, castiro...@gmail.com 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/me....
>
> > > 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 ), {} )