By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

Create a new class on the fly

P: n/a

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.

May 31 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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/2...taclasses.html

--
Steven.

May 31 '07 #2

P: n/a
py_genetic <co************@gmail.comwrote:
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
May 31 '07 #3

P: n/a
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.


Jun 1 '07 #4

P: n/a
py_genetic <co************@gmail.comwrote:
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
Jun 2 '07 #5

P: n/a
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.
Jun 2 '07 #6

P: n/a
Alex Martelli wrote:
>
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.
Alex
Jun 4 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.