Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: class factory

Reply
Thread Tools

Re: class factory

 
 
Alex Martelli
Guest
Posts: n/a
 
      08-20-2003
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

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      08-22-2003
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/Coo...n/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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
use class factory to set required class variables? Alan Python 4 01-27-2011 11:45 AM
Abstract factory and Factory pattern C# ASP .Net 4 07-31-2008 03:22 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
maps and class types: extending a class factory Simon Elliott C++ 0 01-11-2005 01:02 PM
Abstract Factory or Factory Method pattern question.... Medi Montaseri C++ 17 09-03-2003 06:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57