metaclasses

S

Simon Burton

It seems this is a more general way to build classes than inheritance.
Is this a reasonable viewpoint?
.... print "i am meth1"
.... .... print "i am meth2"
....
Simon.
 
G

Gonçalo Rodrigues

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
 
R

Raymond Hettinger

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

... print "i am meth1"
...
... print "i am meth2"
...
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
 
C

Carl Banks

Raymond said:
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.
 
R

Raymond Hettinger

It seems this is a more general way to build classes than inheritance.
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
 
S

Simon Burton

Raymond Hettinger wrote:

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...
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top