本文介绍了公开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的“点"中使用.运算符(属性访问权限)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!