> > Is it possible to have method names of a class generated somehow > dynamically? > > > > More precisely, at the time of writing a program that contains a class > > definition I don't know what the names of its callable methods should > > be. I have entries stored in a database that are changing from time to > > time and I always want to be able to call (from another program) those > > method names which are at the moment present in the database. > > Sounds somehow more like all you need is to learn about __getattr__ and > maybe __call__ instead of actually generating methods. In other words, > don't generate anything, just intercept attempts to call things that > were produced by accessing the attributes of an object. > > Whether that would work or not depends on things you haven't said. The > above spec is a little unclear, given among other things that you don't > "call method names", that the code in the methods you would presumably > want to call has to exist somewhere and you haven't described what that > code would be, and I can't tell what those database "entries" are really > all about other than that they somehow refer to the names of things that > you think you want to call as methods. :-) > > Perhaps an example is in order... >
Thanks for all the replies, it became clear that I need to look into getattr, __getattr__ and __call__. I'll do that, but since you asked, here is the thing I would like to do in a little more detail: My database has 1 table with 2 fields, one called 'name' and the other one called 'age', let's suppose it has the following content, but this content keeps changing: Alice 25 Bob 24 ----------- program1.py ---------------- class klass: # do the stuff with getattr using the database # but in a way that after the database changes # I don't need to rewrite this part inst = klass() ---------- program2.py ------------------ import program1 # This should print 'Hello, my name is Bob and I'm 24' program1.inst.Bob() # This should print 'Hi, I'm 25 and I'm Alice' program1.inst.Alice() # This should print an error message, since there is no # field in the database with name=John program1.inst.John() -- http://mail.python.org/mailman/listinfo/python-list