metaclasses (beginner question)

Discussion in 'Python' started by Laszlo Nagy, Feb 21, 2007.

  1. Laszlo Nagy

    Laszlo Nagy Guest

    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
     
    Laszlo Nagy, Feb 21, 2007
    #1
    1. Advertising

  2. Laszlo Nagy

    Peter Otten Guest

    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
     
    Peter Otten, Feb 21, 2007
    #2
    1. Advertising

  3. Laszlo Nagy

    Peter Otten Guest

    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
     
    Peter Otten, Feb 21, 2007
    #3
  4. Laszlo Nagy

    Laszlo Nagy Guest


    > 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
     
    Laszlo Nagy, Feb 21, 2007
    #4
  5. Laszlo Nagy

    James Stroud Guest

    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
     
    James Stroud, Feb 22, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    589
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Stephan Diehl

    question about metaclasses

    Stephan Diehl, Jun 30, 2003, in forum: Python
    Replies:
    2
    Views:
    335
    Stephan Diehl
    Jun 30, 2003
  3. Simon Burton

    metaclasses

    Simon Burton, Jul 28, 2003, in forum: Python
    Replies:
    5
    Views:
    382
    Simon Burton
    Jul 30, 2003
  4. =?iso-8859-1?q?Steffen_Gl=FCckselig?=

    Question on metaclasses

    =?iso-8859-1?q?Steffen_Gl=FCckselig?=, Apr 24, 2005, in forum: Python
    Replies:
    9
    Views:
    320
    Reinhold Birkenfeld
    Apr 24, 2005
  5. Darren Dale

    design question, metaclasses?

    Darren Dale, Apr 11, 2009, in forum: Python
    Replies:
    7
    Views:
    299
    Piet van Oostrum
    Apr 14, 2009
Loading...

Share This Page