473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.

May 31 '07 #1
6 1914
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
2
by: Migrant | last post by:
Hi Friends; I want develop a group logic with C# (n tier refraction).I know I must create a Class and inherit it But I don't have enough know-how about develop classes. Somebody can help me? ...
4
by: Joe HM | last post by:
Hello - I have a Base Class where I want a New() implemented that can be called from the outside. This New() should create an instance of the appropriate cDerivedX Class ... The following...
7
by: MarkoH | last post by:
Wsdl.exe /server creates abstract class derived from WebService. Is there a way to create this class at runtime based on some WSDL file given at runtime ? What would be even better - creating...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
24
by: M O J O | last post by:
Hi, Instead of doing this.... Public Class Form1 Public Shared Sub CreateAndShow() Dim f As New Form1 f.Show() End Sub
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.