generating method names 'dynamically'
Peter Hansen
peter at engcorp.com
Thu Jan 26 18:53:22 EST 2006
Daniel Nogradi wrote:
> 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...
-Peter
More information about the Python-list
mailing list