metaclasses (beginner question)

L

Laszlo Nagy

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,bases,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
 
P

Peter Otten

Laszlo said:
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
 
P

Peter Otten

Laszlo said:
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
 
L

Laszlo Nagy

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
 
J

James Stroud

Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top