ishtar2020 wrote: > Hi everyone > > I'm sure this question is kinda stupid and has been answered a few > times before... but I need your help! > > I'm writing a small application where the user can analyze some text > based on a set of changing conditions , and right now I'm stuck on a > point where I'd like to automatically generate new classes that operate > based on those user-defined conditions. > > Is there a way in python to define a class in runtime? For instance, > can I define a class which extends another(that I have previously > defined in some module) , create some instance completely on the fly > and then add/redefine methods to it? > > If affirmative, I've thought of a problem I would maybe have to face: > as the class has been defined by direct input to the python > interpreter, I could only create instances of it on the same session I > entered the definition(because it's not on a module I can load on > future uses) but not afterwards. Is there a way to keep that code? > > Even more newbie paranoia: what would happen if I make that 'on the > fly" object persist via pickle? Where would Python find the code to > handle it once unpickled on another session (once again, i take that no > code with the definition of that instance would exist, as it was never > stored on a module). > > Hope it wasn't too ridiculous an idea. > > Thank you for your time, guys.
You might check out the following link on metaclass programming: http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html The author gives at least one short example of creating a class dynamically and using an instance of it. There are also links to other, related articles and topics at the bottom. --dave -- http://mail.python.org/mailman/listinfo/python-list