473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Metaclasses are not called in subclasses. What did I wrong?

I use Python 2.4.4. Please read the code below:

-----------------------------------------------------------
from new import classobj

def mymeta(name,bas es,clsdict):
print 'meta: %s'%name
return classobj(name,b ases,clsdict)

class A(object):
__metaclass__=m ymeta

class B(A):
pass

-----------------------------------------------------------

This should print

meta: A
meta: B

when classes A and B are created. But only meta: B is missing,
since mymeta() is not called when class B is created.

Related python documentation states that mymeta() must be called when B is
created, since metaclasses are looked up in bases classes if not found in
the dictionary of the class itself.
>From Python 2.4.4's manual: "Otherwise, if there is at least one base class,
its metaclass is used (this looks for a __class__ attribute first and if not
found, uses its type)."

Thanks for your help.

Viktor

Oct 29 '06 #1
2 1509
Létez? wrote:
I use Python 2.4.4. Please read the code below:

-----------------------------------------------------------
from new import classobj

def mymeta(name,bas es,clsdict):
print 'meta: %s'%name
return classobj(name,b ases,clsdict)
mymeta is not a class.
class A(object):
__metaclass__=m ymeta
Throw in

print "A.__class_ _", A.__class__

here and read the sentence from the manual again: The __class__ is 'type',
so no print... side effects are to be expected below.
class B(A):
pass

-----------------------------------------------------------

This should print

meta: A
meta: B

when classes A and B are created. But only meta: B is missing,
since mymeta() is not called when class B is created.

Related python documentation states that mymeta() must be called when B is
created, since metaclasses are looked up in bases classes if not found in
the dictionary of the class itself.
>>From Python 2.4.4's manual: "Otherwise, if there is at least one base
class,
its metaclass is used (this looks for a __class__ attribute first and if
not found, uses its type)."
One way to get the output you expect:
>>class AType(type):
.... def __init__(cls, name, bases, clsdict):
.... print "meta:", name
....
>>class A:
.... __metaclass__ = AType
....
meta: A
>>class B(A): pass
....
meta: B

I tried with classobj first, but that didn't work:
>>from new import classobj
class AType(classobj) : pass
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type 'classobj' is not an acceptable base type

Peter
Oct 29 '06 #2
Létezõ wrote:
I use Python 2.4.4. Please read the code below:

-----------------------------------------------------------
from new import classobj

def mymeta(name,bas es,clsdict):
print 'meta: %s'%name
return classobj(name,b ases,clsdict)

class A(object):
__metaclass__=m ymeta

class B(A):
pass

-----------------------------------------------------------

This should print

meta: A
meta: B

when classes A and B are created. But only meta: B is missing,
since mymeta() is not called when class B is created.

Related python documentation states that mymeta() must be called when B is
created, since metaclasses are looked up in bases classes if not found in
the dictionary of the class itself.
>>From Python 2.4.4's manual: "Otherwise, if there is at least one base class,
its metaclass is used (this looks for a __class__ attribute first and if not
found, uses its type)."
Peter already explained that when __metaclass__ is not directly defined in the
class body, it's not used as the metaclass, but the class of the first base
class is.

This is an inconsistency IMHO, since Python normally respects inherited
attributes just like those defined in the class itself.

But well, you can live with it :)

Georg
Oct 29 '06 #3

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

Similar topics

2
1901
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 the following: One has several (metaclass) modules that implements an interesting feature. Lets say, I have a metaclass L, that adds logging...
3
2079
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: http://members.rogers.com/mcfletch/programming/metaclasses.pdf BTW, for those not on Python-list, be sure to check out David & Michele's newest developerworks article on the topic: ...
2
1675
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 class at runtime, but i don't want to include any special code or definition in the class itself. Also, i want to do it just for some particular...
27
1990
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 were new to the language, but now as > more and more people use them and proclaim their widespread benefits, I have > come to realize that through...
4
2051
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 looked like about 20:20 split on the following syntaxes: 1 def func(arg1, arg2, arg3) : function... 2 def func(arg1, arg2, arg3):...
1
1319
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 not) have javascript additions. The output format should be defined ar runtime (php, php+javascrip, asp, asp+javascript, etc.). Currently I have a...
8
1589
by: Jan-Ole Esleben | last post by:
Hi! I am new to this list, and maybe this is a stupid question, but I can't seem to find _any_ kind of answer anywhere. What I want to do is the following: I want to insert a class variable into a class upon definition and actually use it during definition. Manually, that is possible, e.g.:
37
3912
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as a function
0
234
by: Létezõ | last post by:
I use Python 2.4.4. Please read the code below: ----------------------------------------------------------- from new import classobj def mymeta(name,bases,clsdict): print 'meta: %s'%name return classobj(name,bases,clsdict) class A(object):
4
869
by: Laszlo Nagy | last post by:
Hello, I would like to create a hierarchy classes, where the leaves have a special attribute called "producer_id". In addition, I would like to have a function that can give me back the class assigned to any producer_id value. I tried to implement this with a metaclass, but I failed. Please help me, what I did wrong? Thanks,
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.