公开Java Map在Jython中使用,因此其键可在Python的“点"中使用.运算符(属性访问权限) [英] exposing a Java Map<> in Jython so that its keys are available with Python "dot" operator (attribute access)

查看:111
本文介绍了公开Java Map在Jython中使用,因此其键可在Python的“点"中使用.运算符(属性访问权限)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Jython函数中使用Java中的一些Map<String, Object>.我想通过

We have some Map<String, Object> in Java that I would like to make available into a Jython function. I would like to access the contents via

mymap.foo.bar

而不是

mymap['foo']['bar']

是否有一种方法可以将Map包装在对象中,以便在Jython中具有此行为? (例如,类似于Python中的__getattr__方法,仅在Java中实现)

Is there a way to wrap the Map in an object so that it has this behavior in Jython? (e.g. like the __getattr__ method in Python, only implemented in Java)

推荐答案

我最终实现了这一点:

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

用于同时扩展Map<String, Object>PyObject的对象.

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

这篇关于公开Java Map在Jython中使用,因此其键可在Python的“点"中使用.运算符(属性访问权限)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆