dynamically creating classes from text

T H turian9000 at gmail.com
Tue Jan 24 12:22:00 EST 2012

I’m new to python, sorry if my question is a bit naive, I was
wondering if it is possible to parse some text (ie. from a text file
or say html) and then dynamically create a class?

for example lets say the contents of the text file is:

     functionName: bark  arg1: numberBarks
     functionName: run    arg1: howFast  arg2: howLong

and then from the text dynamically create a class like the one below
(including the functions and its implementation)

class Dog:
 	def bark(self, numberBarks, myArg):
		print(‘numberBarks: ‘ + numberBarks)
                print(‘myArg’ + myArg)
	def run(self, howFast, howLong, myArg):
		print(‘howFast: ‘ + howFast)
                print(‘howLong’ + howLong)
                print(‘myArg’ + myArg)

I know my question is a bit far fetched. Anyhow if it is possible how
is it done?
Thanks so much for any help!

More information about the Python-list mailing list