FAQ
I want to implement a "class of classes", so that, instead of the
usual:

spam = MyClass(eggs)

...I can write

spam = MyClass(ham)(eggs)

...where ham is a parameter that will end up as the value of a class
variable of the class returned by MyClass(ham).

In other words, MyClass is a metaclass: a class whose instances
are themselves classes.

In the immediate use I have for such a metaclass, the parameter is
going to be a list of lists of headers, which is used by the __init__
of the generated class to interpret its inputs.

The standard library's collections.namedtuple needs to do something
similar to this, so I thought I could learn how to do it in Python
by studying its source code. I was surprised to discover that
collections.namedtuple achieves this class parametrization by
generating some source code on the fly, from a template, and exec'ing
it.

This looked to me like a rather un-Pythonic hack, but seeing there
in the venerable collections module suggested to me that maybe this
is actually the best way to achieve this effect in Python. Is this
so? If not, please let me know of a better way.

TIA!

kj

Search Discussions

  • Andreas Waldenburger at Oct 4, 2010 at 4:05 pm

    On Mon, 4 Oct 2010 15:59:51 +0000 (UTC) kj wrote:

    I want to implement a "class of classes", so that, instead of the
    usual:

    spam = MyClass(eggs)

    ...I can write

    spam = MyClass(ham)(eggs)
    Use a factory function:

    def MyClass(param):
    class TemplateClass:
    # Do stuff with param.
    return TemplateClass

    /W

    --
    INVALID? DE!
  • Arnaud Delobelle at Oct 4, 2010 at 5:07 pm

    kj <no.email at please.post> writes:

    I want to implement a "class of classes", so that, instead of the
    usual:

    spam = MyClass(eggs)

    ...I can write

    spam = MyClass(ham)(eggs)

    ...where ham is a parameter that will end up as the value of a class
    variable of the class returned by MyClass(ham).

    In other words, MyClass is a metaclass: a class whose instances
    are themselves classes.

    In the immediate use I have for such a metaclass, the parameter is
    going to be a list of lists of headers, which is used by the __init__
    of the generated class to interpret its inputs.

    Just use a class factory, i.e. a function that returns a class.
    The standard library's collections.namedtuple needs to do something
    similar to this, so I thought I could learn how to do it in Python
    by studying its source code. I was surprised to discover that
    collections.namedtuple achieves this class parametrization by
    generating some source code on the fly, from a template, and exec'ing
    it.

    This looked to me like a rather un-Pythonic hack, but seeing there
    in the venerable collections module suggested to me that maybe this
    is actually the best way to achieve this effect in Python. Is this
    so? If not, please let me know of a better way.
    I presume this is done for performance reasons.

    --
    Arnaud

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 4, '10 at 3:59p
activeOct 4, '10 at 5:07p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase