Re: class factory

Discussion in 'Python' started by Alex Martelli, Aug 20, 2003.

  1. Nick Keighley wrote:

    > Hi,
    >
    > I'm a Python beginner and I'm would like to write a function that
    > returns a class (perhaps a tad ambitious...). I've looked through
    > the FAQ and perused "Python In A Nutshell" (not a good book to
    > start with?). The only example I found in PiaN used a simple


    If you're an experienced programmer with other languages then
    the Nutshell should be helpful -- if you're a newbie to programming,
    you should start with easier books (but with the kind of tasks
    you're setting yourself I guess you aren't). I didn't particularly
    emphasize metaprogramming in the Nutshell -- it's more of a "gee
    whiz" kind of thing and the Nutshell aims to cover solid, everyday,
    bread-and-butter usage.

    > if statement to return one of a selection of pre-existing classes.
    > I'd like to generate a class on-the-fly from a parameter (a dictionary).


    How do you want to use that dict -- as the class's dict? Then either:

    def makeaclass(fromdict):
    class X: pass
    X.__dict__ = fromdict
    return X

    or

    def makeaclass(fromdict):
    class X: pass
    X.__dict__.update(fromdict)
    return X

    might be helpful.


    > Can Python do this sort of stuff? Does the mean I have to mess with
    > the dreaded meta-classes?


    Metaclasses may be easier to use than sufficiently-general class-building
    functions, actually -- no reason to dread them. But anyway, yes, Python
    IS pretty good at metaprogramming, too, both with and without metaclasses.


    Alex
     
    Alex Martelli, Aug 20, 2003
    #1
    1. Advertising

  2. Alex Martelli <> wrote in message news:<5SR0b.21604$>...
    > Nick Keighley wrote:


    thamks all for help and suggestions.

    > > I'm a Python beginner and I'm would like to write a function that
    > > returns a class (perhaps a tad ambitious...). I've looked through
    > > the FAQ and perused "Python In A Nutshell" (not a good book to
    > > start with?). The only example I found in PiaN used a simple

    >
    > If you're an experienced programmer with other languages then
    > the Nutshell should be helpful -- if you're a newbie to programming,
    > you should start with easier books (but with the kind of tasks
    > you're setting yourself I guess you aren't).


    no, not a programming beginner. A Python beginner. I was specifically
    trying to do something that was hard (impossible) to do in "normal"
    language (eg. C). The idea was try and generate types (ie. classes)
    that had a
    limited set of values. I'd found the C enum emulators in

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/67107

    but they didn't quite do what I wanted.

    > [...] I didn't particularly
    > emphasize metaprogramming in the Nutshell -- it's more of a "gee
    > whiz" kind of thing and the Nutshell aims to cover solid, everyday,
    > bread-and-butter usage.
    >
    > > if statement to return one of a selection of pre-existing classes.
    > > I'd like to generate a class on-the-fly from a parameter (a dictionary).

    >
    > How do you want to use that dict -- as the class's dict? Then either:
    >
    > def makeaclass(fromdict):
    > class X: pass
    > X.__dict__ = fromdict
    > return X
    >
    > or
    >
    > def makeaclass(fromdict):
    > class X: pass
    > X.__dict__.update(fromdict)
    > return X
    >
    > might be helpful.
    >
    >
    > > Can Python do this sort of stuff? Does the mean I have to mess with
    > > the dreaded meta-classes?

    >
    > Metaclasses may be easier to use than sufficiently-general class-building
    > functions, actually -- no reason to dread them. But anyway, yes, Python
    > IS pretty good at metaprogramming, too, both with and without metaclasses.



    --
    Nick Keighley
     
    Nick Keighley, Aug 22, 2003
    #2
    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. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  2. Medi Montaseri
    Replies:
    17
    Views:
    895
    Medi Montaseri
    Sep 3, 2003
  3. Simon Elliott
    Replies:
    0
    Views:
    385
    Simon Elliott
    Jan 11, 2005
  4. C#
    Replies:
    4
    Views:
    421
  5. Alan
    Replies:
    4
    Views:
    259
    Bill Felton
    Jan 27, 2011
Loading...

Share This Page