473,471 Members | 4,095 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

metaclasses (beginner question)


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,

Laszlo
class ProducerHandlerType(type):
producer_handlers = {}
@classmethod
def registerProducerHandler(cls,producer_id):
ph = cls.producer_handlers
if ph.has_key(producer_id):
ccls = ph[producer_id]
raise Exception("This producer already has a calculator:
"+str(ccls))
else:
print "Registering "+str(cls)+" for producer_id=",producer_id
ph[producer_id] = cls

def __init__(cls,name,bases,dct):
super(ProducerHandlerType,cls).__init__(cls,name,b ases,dct)
if hasattr(cls,'producer_id'):
cls.registerProducerHandler(getattr(cls,'producer_ id'))
else:
print "No producer_id for ",str(cls)
class A(ProducerHandlerType):
pass

class B(A):
producer_id = 1

class C(A):
producer_id = 2

# Metaclass methods are not called above, and the line below prints an
empty dict. :-(
print B.producer_handlers
Feb 21 '07 #1
4 867
Laszlo Nagy wrote:
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?
class ProducerHandlerType(type):
...
class A(ProducerHandlerType):
pass

class B(A):
producer_id = 1
# Metaclass methods are not called above, and the line below prints an
empty dict. :-(
Without looking into the details -- the (subclass of) type is meant to be
the class of the class, or the other way round, your normal classes are
instances of (a subclass of) type. You determine the factory Python uses to
make a class by adding

__metaclass__ = factory

to the class body, so you'll probably end with something like

class ProducerHandlerType(type):
# your code

class A:
__metaclass__ = ProducerHandlerType

The subclasses of A will now your invoke customised metaclass machinery.

Peter
Feb 21 '07 #2
Laszlo Nagy wrote:
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?
class ProducerHandlerType(type):
...
class A(ProducerHandlerType):
pass

class B(A):
producer_id = 1
# Metaclass methods are not called above, and the line below prints an
empty dict. :-(
Without looking into the details -- the (subclass of) type is meant to be
the class of the class, or the other way round, your normal classes are
instances of (a subclass of) type. You determine the factory Python uses to
make a class by adding

__metaclass__ = factory

to the class body, so you'll probably end with something like

class ProducerHandlerType(type):
# your code

class A:
__metaclass__ = ProducerHandlerType

The subclasses of A will now invoke your customised metaclass machinery.

Peter
Feb 21 '07 #3
Without looking into the details -- the (subclass of) type is meant to be
the class of the class, or the other way round, your normal classes are
instances of (a subclass of) type. You determine the factory Python uses to
make a class by adding

__metaclass__ = factory

to the class body, so you'll probably end with something like

class ProducerHandlerType(type):
# your code

class A:
__metaclass__ = ProducerHandlerType

The subclasses of A will now invoke your customised metaclass machinery.
Thanks, I missed that. I added the __metaclass__ line and it worked like
a charm.

Laszlo

Feb 21 '07 #4
Peter Otten wrote:
You determine the factory Python uses to
make a class by adding

__metaclass__ = factory

to the class body, so you'll probably end with something like

class ProducerHandlerType(type):
# your code

class A:
__metaclass__ = ProducerHandlerType

The subclasses of A will now invoke your customised metaclass machinery.
This is the most perfectly succinct description of the __metaclass__
attribute I have ever seen. Beautiful.

James
Feb 22 '07 #5

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...
5
by: Simon Burton | last post by:
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): .... ...
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: ...
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...
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...
9
by: Steffen Glückselig | last post by:
Hello, I've been experimenting with metaclasses a bit (even though I am quite a newbie to python) and stumpled over the following problem in my code: class Meta(type): def __init__(cls, name,...
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: 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
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...
1
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.