Re: [Py4j-users] How to access attributes ?
Status: Beta
Brought to you by:
barthe
From: Barthelemy D. <bar...@in...> - 2014-12-05 13:35:24
|
Hi, So I made a small mistake when replying, but the documentation is actually correct. "get_field" is a function declared in the py4j.java_gateway module and not a method of the JavaGateway object. So you need to call: from py4j.java_gateway import get_field get_field(obj, "field_name") Also, this only works for publicly accessible fields. Private/Package/Protected fields won't be accessible. For instance, py4j.examples.Stack has no public fields. When you do "stack.test3", py4j cannot resolve "test3" to a field, so instead, it creates a JavaMember in an attempt to resolve it later (ex: maybe it's a method, an internal class, etc.). Hope this helps, Barthelemy On Thu Dec 04 2014 at 9:09:53 PM Wilfried Fauvel <wil...@gm...> wrote: > Hi, > > thank you for your fast answer ! > > I added : c = gateway.get_field(stack, 'test3') > but then it raises : > > Traceback (most recent call last): > File "test.py", line 22, in <module> > main() > File "test.py", line 18, in main > c = gateway.get_field(stack, 'test3') > File "/usr/lib/python2.7/site-packages/py4j/java_gateway.py", line > 538, in __call__ > self.target_id, self.name) > File "/usr/lib/python2.7/site-packages/py4j/protocol.py", line 304, > in get_return_value > format(target_id, '.', name, value)) > py4j.protocol.Py4JError: An error occurred while calling t.get_field. > Trace: > py4j.Py4JException: Method get_field([class py4j.examples.Stack, class > java.lang.String]) does not exist > at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:333) > at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:342) > at py4j.Gateway.invoke(Gateway.java:252) > at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133) > at py4j.commands.CallCommand.execute(CallCommand.java:79) > at py4j.GatewayConnection.run(GatewayConnection.java:207) > at java.lang.Thread.run(Thread.java:745) > > and auto_field=True returns <py4j.java_gateway.JavaMember object at > 0x7f2d7ce7aa10> > > I am using : pip2.7 show py4j > --- > Name: py4j > Version: 0.8.2.1 > Location: /usr/lib/python2.7/site-packages > Requires: > > and > > java version "1.7.0_71" > OpenJDK Runtime Environment (IcedTea 2.5.3) (Arch Linux build > 7.u71_2.5.3-1-x86_64) > OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode) > > Thank you. > > On Thu, Dec 4, 2014 at 5:23 AM, Barthelemy Dagenais > <bar...@in...> wrote: > > Hi! > > > > Thanks for using Py4J! > > > > To access fields, you can either use gateway.get_field(object, "test1") > or > > set auto_field=True when initializing the gateway to access object.test1. > > > > Reference: http://py4j.sourceforge.net/faq.html#how-to-access-a-field > > > > auto_field is False by default because it costs an extra lookup. > > > > Barthelemy > > > > On Wed Dec 03 2014 at 10:30:12 PM Wilfried Fauvel > > <wil...@gm...> wrote: > >> > >> Hi, I am testing py4j and I would like to know if it is possible to > >> access class attributes. > >> In the Stack class from the getting started page, I added : > >> > >> public static final String test1 = "TEST"; > >> public static final int test2 = 12; > >> public int test3 = 12; > >> > >> and with python 2.7 : > >> > >> gateway = JavaGateway() > >> stack = gateway.entry_point.getStack() > >> print(gateway.help(stack)) > >> >>> field section is empty > >> > >> print stack.test1 > >> print stack.test2 > >> print stack.test3 > >> >>> <py4j.java_gateway.JavaMember object at 0x7fd7b1647250> > >> >>> <py4j.java_gateway.JavaMember object at 0x7fd7b1647a10> > >> >>> <py4j.java_gateway.JavaMember object at 0x7fd7b16479d0> > >> > >> Thank you, > >> Wilfried. > >> > >> > >> ------------------------------------------------------------ > ------------------ > >> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > >> from Actuate! Instantly Supercharge Your Business Reports and Dashboards > >> with Interactivity, Sharing, Native Excel Exports, App Integration & > more > >> Get technology previously reserved for billion-dollar corporations, FREE > >> > >> http://pubads.g.doubleclick.net/gampad/clk?id=164703151& > iu=/4140/ostg.clktrk > >> _______________________________________________ > >> Py4j-users mailing list > >> Py4...@li... > >> https://lists.sourceforge.net/lists/listinfo/py4j-users > > > > > > ------------------------------------------------------------ > ------------------ > > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > > with Interactivity, Sharing, Native Excel Exports, App Integration & more > > Get technology previously reserved for billion-dollar corporations, FREE > > http://pubads.g.doubleclick.net/gampad/clk?id=164703151& > iu=/4140/ostg.clktrk > > _______________________________________________ > > Py4j-users mailing list > > Py4...@li... > > https://lists.sourceforge.net/lists/listinfo/py4j-users > > > > ------------------------------------------------------------ > ------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > http://pubads.g.doubleclick.net/gampad/clk?id=164703151& > iu=/4140/ostg.clktrk > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users > |