You can access the attributes from a PyObject
directly by using the __getattr__(PyString)
and __setattr__(PyString, PyObject)
methods.
You can get an attribute with:
PyObject pyObject = ...;
// Get attribute: value = getattr(obj, name)
// OR: value = obj.__getattr__(name)
PyString attrName = Py.newString("some_attribute");
PyObject attrValue = pyObject.__getattr__(attrName);
- WARNING: Make sure to use
__getattr__(PyString)
because__getattr__(String)
only works for interned strings.
You can also set an attribute with:
PyObject pyObject = ...;
// Set attribute: setattr(obj, name, value)
// OR: obj.__setattr__(name, value)
PyString attrName = Py.newString("some_attribute");
PyObject attrValue = (PyObject)Py.newString("A string as the new value.");
pyObject.__setattr__(attrName, attrValue);
NOTE: The value does not have to be a
PyString
. It just has to be aPyObject
.WARNING: Make sure to use
__setattr__(PyString, PyObject)
because__setattr__(String, PyObject)
only works for interned strings.
Also, you can call a python method using __call__(PyObject[] args, String[] keywords)
:
PyObject pyObject = ...;
// Get method: method = getattr(obj, name)
// OR: method = obj.__getattr__(name)
PyString methodName = Py.newString("some_method");
PyObject pyMethod = pyObject.__getattr__(methodName);
// Prepare arguments.
// NOTE: args contains positional arguments followed by keyword argument values.
PyObject[] args = new PyObject[] {arg1, arg2, ..., kwarg1, kwarg2, ...};
String[] keywords = new String[] {kwname1, kwname2, ...};
// Call method: result = method(arg1, arg2, ..., kwname1=kwarg1, kwname2=kwarg2, ...)
PyObject pyResult = pyMethod.__call__(args, keywords);
- NOTE: I cannot explain why the keyword names are
String
here when getting an attribute by name requiresPyString
.