Jython by Keyword-Parameter passing to invoke Java methods
Question: I have this Java class,

    
    
    public class sample {
        public Integer foo(Integer x){
            return x+5;
        }
    }
    

And with Jython I want to call `.foo` while passing "keyword-parameter" to the
argument. I ended up with the following Python code,

    
    
    java_file = sample()
    kwargs = {'x':3}
    print java_file.foo(**kwargs)
    

But this results in an error,

    
    
    print java_file.foo(**kwargs)
    TypeError: foo(): takes no keyword arguments
    

Is there a way to do this? Thanks


Answer: From this [Jython FAQ](http://www.jython.org/archive/22/userfaq.html):

> **5.3 Supporting *args and **kw in Java methods**
>
> In Jython you can support keyword arguments on Java methods by defining a
> method like so (the parameters are the important point):
>  
>  
>     public PyObject foo(PyObject[] args, String[] keywords);
>  
>
> The keywords array contains all of the keywords for the keyword-defined
> arguments. For example, if you called foo with:
>  
>  
>     foo(1,2,3,four=4,five=5)
>  
>
> args would be: [1, 2, 3, 4, 5] and keywords would be: ['four', 'five'] (an
> array of 2 elements.)
>
> Additionally, you can use the experimental argument parser
> org.python.core.ArgParser to deal with mapping these two arrays. Consult the
> Javadocs (or source) for further details on org.python.core.ArgParser.

In the example above, the sample class might look like this:

    
    
    public class sample {
        public Integer foo(PyObject[] args, String[] keywords) {
            ArgParser argParser = new ArgParser("foo", args, keywords, "x");
            Integer x = argParser.getInt(0);
            return x + 5;
        }
    }
    

