473,395 Members | 1,653 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,395 software developers and data experts.

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,bases,clsdict):
print 'meta: %s'%name
return classobj(name,bases,clsdict)

class A(object):
__metaclass__=mymeta

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 1501
Létez? wrote:
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)
mymeta is not a class.
class A(object):
__metaclass__=mymeta
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,bases,clsdict):
print 'meta: %s'%name
return classobj(name,bases,clsdict)

class A(object):
__metaclass__=mymeta

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
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...
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...
8
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...
37
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...
0
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...
4
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.