Dynamic method invocation

jythonuser anishaapte at gmail.com
Fri Jun 26 18:11:12 EDT 2009


Sorry for being a little vague.  The last part of your response seems
like what I would need.  So here are more details on what I am trying
to do.

I have an extensible command shell in java where commmand providers
plug in to supply new commands.  I have a java class called MyShell
which has execCommand method on it.  Thus MyShell class does not have
a concrete method on it for each command that it supports (e.g. grep,
ls, cat etc) but a generic execCommand method that takes the name of
the command and command params as arguments.

clas MyShell () {
    Object execCommand (cmdName, params) {
        Object o = // find command object from command name
        return o.invoke (cmdName, params);
    }
|

Now I want to use this in jython.  So I could do something like
shell = MyShell()
shell.exec ("grep", grep_args)

What I would like to do is -
shell.grep (grep_args)

That way my users don't have to use some gorpy exec/invoke syntax for
each command but think that shell has all the commands defined on it.
So based on your example I am hoping that I can use getattr on "some"
object that returns me "some" method that I can call.

Does this make sense and is there a way to do this in jython?



More information about the Python-list mailing list