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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |