Re: `PySet` object recognized as `str` object
Frank Dai <[email protected]>
| Newsgroups | gmane.comp.lang.jython.user |
|---|---|
| Message-ID | <CAPJGSt8tRBj4Z5qQ2fZRvpMpiUxjokB=Pc4rBZFyfZsBJWy1qw@mail.gmail.com> |
Hi Jim, You're correct, thank you very much! You're awesome! On Sat, May 9, 2015 at 7:33 AM, Jim Baker <[email protected]> wrote: > Frank, > > The problem is in the line > > final PySet set = new PySet(PyString.TYPE); > > I think you are trying to use this as if it were PySet<PyString> set = new > PySet<PyString>(), but actually it means that the type of the resulting > object in Python code should be PyString. Which is exactly what you observe! > > (Why is the case? We have two object models in Jython: Python and Java, > and we have to combine support in the runtime. It can be a bit tricky to do > so!) > > The solution is very simple: change the initialization to > > final PySet set = new PySet() > > - Jim > > On Sat, May 9, 2015 at 8:22 AM, Frank Dai <[email protected]> wrote: > >> The following is my java code: >> >> public void testPySet() { >> final PythonInterpreter interpreter = new PythonInterpreter(); >> final String userCode = "def >> test(x):\n\tprint(x)\n\tx.add(\"d\")\n\treturn x"; >> interpreter.exec(userCode); >> final PyObject method = interpreter.get("test"); >> final PySet set = new PySet(PyString.TYPE); >> set.add("a"); >> set.add("b"); >> set.add("c"); >> final PyObject result = method.__call__(set); >> System.out.println(result); >> } >> >> When I run it, it will throw out an error message: >> >> Traceback (most recent call last): >> File "<string>", line 3, in test >> AttributeError: 'str' object has no attribute 'add' >> >> If I remove the x.add(\"d\"), then it will run successfully. >> >> It’s very weird that the PySet object is recognized as an str object. >> >> Does anyone happen to know why? >> >> >> ------------------------------------------------------------------------------ >> One dashboard for servers and applications across Physical-Virtual-Cloud >> Widest out-of-the-box monitoring support with 50+ applications >> Performance metrics, stats and reports that give you Actionable Insights >> Deep dive visibility with transaction tracing using APM Insight. >> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y >> _______________________________________________ >> Jython-users mailing list >> [email protected] >> https://lists.sourceforge.net/lists/listinfo/jython-users >> > > > > -- > - Jim > > jim.baker@{colorado.edu|python.org|rackspace.com|zyasoft.com} > twitter.com/jimbaker > github.com/jimbaker > bitbucket.com/jimbaker > -- My tech blog: http://www.soulmachine.me My GitHub: https://github.com/soulmachine My LinkedIn: http://www.linkedin.com/in/soulmachine/ My Sina Weibo: http://weibo.com/soulmachine ------------------------------------------------------------------------------ One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y _______________________________________________ Jython-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/jython-users