473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

metaclasses


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.

Jul 18 '05 #1
5 1445
On Tue, 29 Jul 2003 08:25:26 +1000, Simon Burton
<si****@webone.com.au> 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
Jul 18 '05 #2
> 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
Jul 18 '05 #3
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
Jul 18 '05 #4
> >> 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


Jul 18 '05 #5
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.

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
3
by: Mike C. Fletcher | last post by:
Slides from my PyGTA presentation on Tuesday, focusing mostly on why/where you would want to use meta-classes, are available in PDF format: ...
2
by: Santiago Aguiar | last post by:
Im trying to use AOP techniques on python but, even if I understand the idea behind metaclasses, I cant find a way to use AOP in a dynamic way. What im trying to do is to weave an aspect to a...
27
by: Michele Simionato | last post by:
> Hello, my name is Skip and I am metaclass-unaware. I've been programming in > Python for about ten years and I have yet to write a metaclass. At first I > thought it was just that metaclasses...
4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
13
by: Jean-François Doyon | last post by:
Hello, I'm using MetaClasses to create classes. How do I make these new classes "globally" available? I probably just have to assign them to something magic, but I can't seem to figure out...
1
by: Hartmut Goebel | last post by:
Hi, I'm currently implementing a tool for auto-generating webforms from a desription file. The output should become a php-script, an asp-script, zope-formulator or such. Each if it may (or may...
2
by: Paul Morrow | last post by:
....that they warrant an entirely new syntax? It seems that they are very similar in a very significant way --- they alter the default behavior of something. IMO, it's not a stretch to say that...
0
by: Jan-Ole Esleben | last post by:
Hi! I've just posted a question about metaclasses in ZOPE on the ZOPE list, and one of the replies said that metaclasses (at least "painless" metaclasses) cannot be used without new-style...
1
by: Alan Isaac | last post by:
Forman's book is out of print. Is there a good substitute? Thanks, Alan Isaac
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.