Create a new class on the fly

P

py_genetic

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.
 
S

Steven D'Aprano

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
 
A

Alex Martelli

py_genetic said:
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
 
P

py_genetic

Alex, thanks for the advise:
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.
 
A

Alex Martelli

py_genetic said:
Alex, thanks for the advise:


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
 
A

Anton Vredegoor

Alex said:
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.
 
J

Josh West

Alex said:
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,
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top