Repository navigation
Accessing Java variables with illegal Python characters #1424
Replies: 18 comments
|
You use getattr and setattr for variables that are not directly available.
public class Test
{
public int foo_$bar;
}
…---
import jpype
jpype.startJVM(classpath=".")
Test = jpype.JClass("Test")
test = Test()
print(getattr(test,"foo_$bar"))
print(setattr(test,"foo_$bar", 2))
print(getattr(test,"foo_$bar"))
|
|
Thanks! |
|
Please note that you may use getatter/setattr with the JClass customizer (_jclass_init_) to rename variables to new names. Thus they don’t have to be inaccessible in Python. You can rename it foo_DDbar if you wanted to. Simply choose the root of your tree and apply a customizer routine that will get the map of fields and method and assign them to the correct Python bindings. That is how JPype creates Pythonic classes from uncooperative Java classes.
|
|
Thanks, i'll give this a try. |
|
This mechanism is somewhat working when a Java object is instantiated, but I am unsure how to get it to work with things like: public interface Constants {
public static final String FOO_BAR = "blah";
}If I just do |
|
Interesting. I though that it ran at class initiation. I will take a look. |
|
Sorry this got delayed due to cataract surgery. Did you figure it out? |
|
No worries, I hope the surgery went well for you. I never figured this out. I was hoping you could try my little example above to see that |
|
I had no difficulty with the rename. Are you sure you don't have a problem with your pattern of applying the customizer? |
|
Note you don't need to care about order here. |
|
In the case that wasn't working for me, I never instantiated an object. I was just working with static class variables: public interface Constants {
public static final String FOO_BAR = "blah";
}Would you expect the |
|
I should add that it works for me with instantiated object member variables, so I think my pattern of applying is working. |
|
Okay I see the problem. I did not realize it was a static field. Those are very hard to manipulate as unlike an instance you need to set the value in the static parent without dereferencing. I can try to make a work around in a future version, but for now the best we can do is something indirect. With |
|
I made a pattern to make this much easy to access. |
|
I am not sure if I want to hold off the current release to add the descriptor manipulator, but you can try It out see if it is a better solution for your long term use case.
|
|
Will this result in |
Yes, please don't hold up the release on account of this. |
|
Yes, this command will be callable from the The problem with the current implementation is we can only set members whuch start with underscore. For an instance this is fine as it gets dereferenced on access. But for a static, it sets in the python class, not java side. Effectively you can't set any static field that leads with underscore properly. Total disaster! So I need to change the logic to use a new "descriptor magic" to resolve the it. |
Uh oh!
There was an error while loading. Please reload this page.
Hopefully this is a question with an answer instead of an issue.
If I am trying to access the Java variable
MyClass.foo_$barfrom JPype, how to I do that? Including the$results inSyntaxError: invalid syntax.All reactions