metaclasses

Discussion in 'Python' started by Simon Burton, Jul 28, 2003.

  1. Simon Burton

    Simon Burton Guest

    It seems this is a more general way to build classes than inheritance.
    Is this a reasonable viewpoint?

    >>>
    >>> def meth1(self):

    .... print "i am meth1"
    ....
    >>> def meth2(self):

    .... print "i am meth2"
    ....
    >>> Foo = type("Foo",(),{'meth':meth1}) # make a class
    >>> foo = Foo() # make an instance
    >>> foo.meth() # call a method

    i am meth1
    >>>


    Simon.
     
    Simon Burton, Jul 28, 2003
    #1
    1. Advertising

  2. On Tue, 29 Jul 2003 08:25:26 +1000, Simon Burton
    <> wrote:

    >
    >It seems this is a more general way to build classes than inheritance.
    >Is this a reasonable viewpoint?
    >


    Yes, it's reasonable. A class statement is syntactic sugar for a call
    to the type constructor. As for the usefullness of the POV that's a
    whole different matter:

    "Metaclasses are deeper magic than 99% of users should ever worry
    about. If you wonder whether you need them, you don't (the people who
    actually need them know with certainty that they need them, and don't
    need an explanation about why)." -- Tim Peters

    Mr. Tim Peters is always right.

    With my best regards,
    G. Rodrigues
     
    Gonçalo Rodrigues, Jul 29, 2003
    #2
    1. Advertising

  3. > It seems this is a more general way to build classes than inheritance.
    > Is this a reasonable viewpoint?
    >
    > >>>
    > >>> def meth1(self):

    > ... print "i am meth1"
    > ...
    > >>> def meth2(self):

    > ... print "i am meth2"
    > ...
    > >>> Foo = type("Foo",(),{'meth':meth1}) # make a class
    > >>> foo = Foo() # make an instance
    > >>> foo.meth() # call a method

    > i am meth1



    It is a less general way for building classes.

    Using inheritance is more general because it allows
    you to override the methods of a type object and
    provide a custom __repr__, __getattribute__,
    __setattr__, __init__, __new__, or some such.


    Raymond Hettinger
     
    Raymond Hettinger, Jul 29, 2003
    #3
  4. Simon Burton

    Carl Banks Guest

    Raymond Hettinger wrote:
    >> It seems this is a more general way to build classes than inheritance.
    >> Is this a reasonable viewpoint?
    >>
    >> >>>
    >> >>> def meth1(self):

    >> ... print "i am meth1"
    >> ...
    >> >>> def meth2(self):

    >> ... print "i am meth2"
    >> ...
    >> >>> Foo = type("Foo",(),{'meth':meth1}) # make a class
    >> >>> foo = Foo() # make an instance
    >> >>> foo.meth() # call a method

    >> i am meth1

    >
    > It is a less general way for building classes.


    Actually, the question doesn't make sense. Metaclasses and
    inheritance are two different dimensions of generality. Neither is
    more or less general than the other, because they generalize in
    different ways.

    However, his use of the built-in metatype 'type' is more general than
    the class statement, which seemed to be what he was asking. One can
    supply a list of bases which, unlike the class statement, need not be
    a fixed length. In fact, the bases can be calculated dynamically.
    Likewise with the class dict.


    --
    CARL BANKS
     
    Carl Banks, Jul 30, 2003
    #4
  5. > >> It seems this is a more general way to build classes than inheritance.
    > >> Is this a reasonable viewpoint?


    > > It is a less general way for building classes.

    >
    > Actually, the question doesn't make sense. Metaclasses and
    > inheritance are two different dimensions of generality. Neither is
    > more or less general than the other, because they generalize in
    > different ways.
    >
    > However, his use of the built-in metatype 'type' is more general than
    > the class statement, which seemed to be what he was asking. One can
    > supply a list of bases which, unlike the class statement, need not be
    > a fixed length. In fact, the bases can be calculated dynamically.
    > Likewise with the class dict.


    I read his question as asking about the merits
    of subclassing type vs using type() directly.

    class MyMeta(type):
    . . .
    class MyClass:
    __metatype__ = MyMeta
    . . .

    vs.

    MyClass = type(n, b, d)



    Raymond Hettinger
     
    Raymond Hettinger, Jul 30, 2003
    #5
  6. Simon Burton

    Simon Burton Guest

    On Wed, 30 Jul 2003 00:17:54 +0000, Carl Banks wrote:

    > Raymond Hettinger wrote:


    >>
    >> It is a less general way for building classes.

    >
    > Actually, the question doesn't make sense. Metaclasses and
    > inheritance are two different dimensions of generality. Neither is
    > more or less general than the other, because they generalize in
    > different ways.
    >
    > However, his use of the built-in metatype 'type' is more general than
    > the class statement, which seemed to be what he was asking. One can
    > supply a list of bases which, unlike the class statement, need not be
    > a fixed length. In fact, the bases can be calculated dynamically.
    > Likewise with the class dict.


    oh, whoops...

    >>>
    >>> a = ()
    >>> class A(*a):

    File "<stdin>", line 1
    class A(*a):
    ^
    SyntaxError: invalid syntax
    >>>


    The only thing i can think of right now is being able to
    plug in different collections of methods depending on "what
    the client code needs". This would require an exec and mixins
    for each method, or (better) assigning functions to class attributes,
    perhaps even accessing the class __dict__.

    It's still not clear to me this metaclass stuff. My application
    domain is, umm, symbolic algebra, term rewriting etc.

    Simon Burton.
     
    Simon Burton, Jul 30, 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. Stephan Diehl

    question about metaclasses

    Stephan Diehl, Jun 30, 2003, in forum: Python
    Replies:
    2
    Views:
    336
    Stephan Diehl
    Jun 30, 2003
  2. 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
  3. Simon Burton

    metaclasses for type comparison

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

    AOP and metaclasses

    Santiago Aguiar, Feb 20, 2004, in forum: Python
    Replies:
    2
    Views:
    326
    John Roth
    Feb 20, 2004
  5. Aahz
    Replies:
    12
    Views:
    524
    Dave Benjamin
    Apr 5, 2004
Loading...

Share This Page