hsauro 40 Posted January 26, 2022 I'm using DelphiWrapper to expose an API in a Delphi application that Python can run methods from. So far it works ok. It's a very primitive plugin system. My question is whether there is a way for a Delphi method to return a python list to the python caller using delphiwrapper. eg In python: mylist = delphiAPI.getlist() where mylist might be a list of strings ['red', 'dog', 'rock'] At the moment I export two more primitive methods, one method to get the number of items and another method to get the ith item. I then create a small Python function that builds a python list using those more primitive methods. I was wondering if there was another way to do this using DelphiWrapper. In the long term, it would also be nice to return and pass to Delphi numpy arrays but I imagine that would be much more difficult. Share this post Link to post
pyscripter 689 Posted January 26, 2022 Wrapping of TStrings is fully supported. You can easily turn it into a Python list if needed. Look at Demo 31, python function testTStrings. Share this post Link to post
hsauro 40 Posted January 26, 2022 (edited) That worked, thanks, I should have tried it first. For those who are interested. all I did was declare my method at the Delphi end as: function TObj.getlist : TStringList; begin result := TStringList.Create; result.Add ('abc); etc end I assume the TStringList is reference-counted at the python end and I shouldn't care about dreeing it? Then at the python end, I just called x = obj.getList() It comes back as a TString type but you can convert it to a python list using list(x) or x.ToList(). As a TString type, it does have all the methods you'd expect for a Delphi TString type, eg x.Add ('A long string') Edited January 26, 2022 by hsauro Share this post Link to post
pyscripter 689 Posted January 27, 2022 (edited) 7 hours ago, hsauro said: I assume the TStringList is reference-counted at the python Wrong assumption. You need to call Free on the Python side or free it from Delphi. You can examine the ownership of a Delphi object in Python using the __owned__ attribute. If __owned__ is true it will be freed automatically. When you wrap objects yourself using DelphiWrappper.Wrap, you can specify the ownership. But here you are returning the object as a function result and you cannot tell who owns the object. Objects returned as function results or by accessing properties are wrapped using Wrap(obj, soReference); Edited January 27, 2022 by pyscripter Share this post Link to post
hsauro 40 Posted January 27, 2022 (edited) Thanks, I'll take that into account One other question I have, how can I check if a python script I import has a given function? eg p = Import (mypythoncode) p.myfunction() Is there a check I make to see if myfunction() is defined in mypythoncode other than actually trying to call it and trapping the exception? I should add that myfunction() isn't part of a class. Edited January 27, 2022 by hsauro Share this post Link to post
pyscripter 689 Posted January 27, 2022 2 hours ago, hsauro said: One other question I have, how can I check if a python script I import has a given function? Something like Module := Import(ModuleName) if Module.__dir__().Contains(FunctionName) then Module.FunctionName() Share this post Link to post
hsauro 40 Posted January 27, 2022 1 hour ago, pyscripter said: Something like Module := Import(ModuleName) if Module.__dir__().Contains(FunctionName) then Module.FunctionName() Thanks that worked. Share this post Link to post