Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > metaclasses

Reply
Thread Tools

metaclasses

 
 
Simon Burton
Guest
Posts: n/a
 
      07-28-2003

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.

 
Reply With Quote
 
 
 
 
Gonšalo Rodrigues
Guest
Posts: n/a
 
      07-29-2003
On Tue, 29 Jul 2003 08:25:26 +1000, Simon Burton
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      07-29-2003
> 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


 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      07-30-2003
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
 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      07-30-2003
> >> 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




 
Reply With Quote
 
Simon Burton
Guest
Posts: n/a
 
      07-30-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Prothon, metaclasses, Zope [Was: A 'Python like' language] Aahz Python 12 04-05-2004 03:48 AM
AOP and metaclasses Santiago Aguiar Python 2 02-20-2004 11:51 PM
metaclasses for type comparison Simon Burton Python 5 09-11-2003 09:11 PM
Metaclasses presentation slides available... Mike C. Fletcher Python 3 08-29-2003 02:27 PM
question about metaclasses Stephan Diehl Python 2 06-30-2003 04:04 PM



Advertisments