Skip to content Skip to sidebar Skip to footer

Exposing A Java Map<> In Jython So That Its Keys Are Available With Python "dot" Operator (attribute Access)

We have some Map in Java that I would like to make available into a Jython function. I would like to access the contents via mymap.foo.bar rather than mymap[

Solution 1:

I ended up implementing this:

@Overridepublic PyObject __findattr_ex__(String name) {
    if (this.containsKey(name))
    {
        return Py.java2py(this.get(name));
    }
    else
    {
        throw Py.AttributeError(name);
    }
}    

for an object that extends both Map<String, Object> and PyObject.

Post a Comment for "Exposing A Java Map<> In Jython So That Its Keys Are Available With Python "dot" Operator (attribute Access)"