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 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
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
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
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 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: 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):
.... ...
|
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: 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: 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...
|
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...
|
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,...
|
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...
|
by: Alan Isaac |
last post by:
Forman's book is out of print.
Is there a good substitute?
Thanks,
Alan Isaac
|
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: 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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |