Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

Create a new class on the fly


Is this possible or is there a better way.  I need to create a new
class during runtime to be used inside a function. The class
definition and body are dependant on unknows vars at time of exec,
thus my reasoning here.

class PosRecords(tables.IsDescription):

class A(object):
    self.__init__(self, args):
         ........
    def mkClass(self, args):
          eval( "class B(object): ...") #definition on B is dependant
on dynamic values in string
          ......do stuff with class

thanks.

On Wed, 30 May 2007 17:44:06 -0700, py_genetic wrote:

> Is this possible or is there a better way.  I need to create a new
> class during runtime to be used inside a function. The class
> definition and body are dependant on unknows vars at time of exec,
> thus my reasoning here.

You might want a class factory, a function that returns a class, or a
metaclass, a class that returns classes.

Building the class definition as a string at runtime and then using exec
(not eval) to create the class is probably the most obvious way of doing
it, but it isn't very convenient, and if you are getting any part of the
string from a source not under your control (e.g. a web form) you're
opening yourself up to a world of security holes.

The cleaner, safer, more convenient ways of building classes at runtime is
to use the Python metaclass machinery, perhaps wrapped in a function for
convenience. See here for examples:

http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html

--
Steven.

py_genetic <conor.robin@gmail.com> wrote:
> Is this possible or is there a better way.  I need to create a new
> class during runtime to be used inside a function. The class
> definition and body are dependant on unknows vars at time of exec,
> thus my reasoning here.

> class PosRecords(tables.IsDescription):

> class A(object):
>     self.__init__(self, args):

This makes 0 sense; maybe you should learn elementary Python syntax well
_before_ trying advanced stuff, no?

>          ........
>     def mkClass(self, args):
>           eval( "class B(object): ...") #definition on B is dependant
> on dynamic values in string
>           ......do stuff with class

Just use a class statement, and setattr on the resulting class object to
set stuff dynamically on it.  eval won't do what you want (it takes
expressions, not statements), and exec is an even worse way to go about
it.

E.g.:

class A(object):
  def mkClass(self, name, *k):
    class bah(object): pass
    bah.__name__ = name
    for n in k: setattr(bah, n, k[n])
    return bah

Alex

Alex, thanks for the advise:

> > class PosRecords(tables.IsDescription):

> > class A(object):
> >     self.__init__(self, args):

> This makes 0 sense; maybe you should learn elementary Python syntax well
> _before_ trying advanced stuff, no?

I accidently left that erroneous snippet in, however if your offering
a class in smart ass let me know where to sign up.

py_genetic <conor.robin@gmail.com> wrote:
> Alex, thanks for the advise:

> > > class PosRecords(tables.IsDescription):

> > > class A(object):
> > >     self.__init__(self, args):

> > This makes 0 sense; maybe you should learn elementary Python syntax well
> > _before_ trying advanced stuff, no?

> I accidently left that erroneous snippet in, however if your offering
> a class in smart ass let me know where to sign up.

Thanks for snipping all the actual helpful stuff I posted, it makes SO
much easier for me to be snide!

You can find a few examples of me demonstrating the subject of your
interest by searching for my name e.g. on video.google.com; searching
for my name on Amazon will show some books using similar techniques, and
searching for my name on groups.google.com will find about 50,000 posts
many of which exhibit essentially the same approach.  Unfortunately, I
can't currently offer such courses commercially while staying employed
as Uber Tech Lead for Google, Inc, but if the monies on offer make it
worth my while for me to drop million bucks worth of stock options, plus
a vigorish for my other comp package _and_ the incredible amount of
happiness I get every day from my job (where I get to interact with
truly brlliant people, who, if and when they "leave an erroneous snippet
in", are GRATEFUL to me for pointing it out, rather than RESENTFUL and
DEFENSIVE), I'll surely consider that most seriously (as long as the
monies in question are in escrow for my personal reassurance).

Until such conditions should obtain, I'll just have to keep freely
helping the people who are WORTH helping, and poking sarcastic funs at
those who prove themselves ot be a waste of oxygen instead.

May you have the life you deserve,

Alex

Alex Martelli wrote:
> You can find a few examples of me demonstrating the subject of your
> interest by searching for my name e.g. on video.google.com; searching
> for my name on Amazon will show some books using similar techniques, and
> searching for my name on groups.google.com will find about 50,000 posts
> many of which exhibit essentially the same approach.  Unfortunately, I
> can't currently offer such courses commercially while staying employed
> as Uber Tech Lead for Google, Inc, but if the monies on offer make it
> worth my while for me to drop million bucks worth of stock options, plus
> a vigorish for my other comp package _and_ the incredible amount of
> happiness I get every day from my job (where I get to interact with
> truly brlliant people, who, if and when they "leave an erroneous snippet
> in", are GRATEFUL to me for pointing it out, rather than RESENTFUL and
> DEFENSIVE), I'll surely consider that most seriously (as long as the
> monies in question are in escrow for my personal reassurance).

And still you are not bored with yourself? What a waste.

> Until such conditions should obtain, I'll just have to keep freely
> helping the people who are WORTH helping, and poking sarcastic funs at
> those who prove themselves ot be a waste of oxygen instead.

So you have found out about the trick of never being wrong, and what's
worse you now have a large group of followers continually reinforcing
you and thus keeping you stuck in the psychic plane.

> May you have the life you deserve,

It seems you already have it.

A.

I'm new to all this, but I didn't get the impression that sickeningly
smug self-satisfaction was a "pythonic" characteristic. Let's hope for a
Waco (cults, siege, bloodbath) style conclusion to the Google story.
That would be truly (Monty) Pythonic.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc