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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to