Python4Delphi:在函数中返回python对象。(德尔菲包装)

Python4Delphi:在函数中返回python对象。(德尔菲包装),delphi,delphi-xe2,python4delphi,Delphi,Delphi Xe2,Python4delphi,我用的是蟒蛇。如何从包装好的Delphi类函数返回对象 代码段: 我有一个简单的Delphi类,我把它包装成Python脚本,对吗 TSimple = Class Private function getvar1:string; Public Published property var1:string read getVar1; function getObj:TSimple; end; ... function TSimple.getVar1:string; begin

我用的是蟒蛇。如何从包装好的Delphi类函数返回对象

代码段:

我有一个简单的Delphi类,我把它包装成Python脚本,对吗

TSimple = Class
Private
  function getvar1:string;
Public    
Published
  property var1:string read getVar1;
  function getObj:TSimple;
end;
... 
function TSimple.getVar1:string;
begin
  result:='hello';
end;
function TSimple.getObj:TSimple;
begin
  result:=self;
end;
我制作了与demo32类似的TPySimple,以使类能够访问Python代码。我的Python模块名是test

TPySimple = class(TPyDelphiPersistent)
  // Constructors & Destructors
  constructor Create( APythonType : TPythonType ); override;
  constructor CreateWith( PythonType : TPythonType; args : PPyObject ); override;
  // Basic services
  function  Repr : PPyObject; override;

  class function  DelphiObjectClass : TClass; override;
end;
...

{ TPySimple }

constructor TPySimple.Create(APythonType: TPythonType);
begin
  inherited;
  // we need to set DelphiObject property
  DelphiObject := TSimple.Create;
  with TSimple(DelphiObject) do begin
  end;
  Owned := True; // We own the objects we create
end;

constructor TPySimple.CreateWith(PythonType: TPythonType; args: PPyObject);
begin
  inherited;
  with GetPythonEngine, DelphiObject as TSimple do
    begin
      if PyArg_ParseTuple( args, ':CreateSimple' ) = 0 then
        Exit;
    end;
end;

class function TPySimple.DelphiObjectClass: TClass;
begin
  Result := TSimple;
end;

function TPySimple.Repr: PPyObject;
begin
  with GetPythonEngine, DelphiObject as TSimple do
    Result := VariantAsPyObject(Format('',[]));
    // or Result := PyString_FromString( PAnsiChar(Format('()',[])) );
end;
现在是python代码:

import test

a = test.Simple()
# try access the property var1 and everything is right
print a.var1
# work's, but..
b = a.getObj();
# raise a exception that not find any attributes named getObj.
# if the function returns a string for example, it's work.

根据OP,他在这里找到了答案:

(复制粘贴供参考)

我有一个建议——利用D2010(及以上版本)中新的RTTI(运行时类型信息)特性,让Delphi对象轻松地暴露在python中

目前,向托管Python代码公开类需要编写太多的代码(请检查demo06),我想如果我们在较新版本的Delphi中利用新的RTTI特性,这个过程可以得到很大改进

例如,查看Delphi chromium嵌入式项目,要将任何Delphi类的接口公开给JavaScript环境,只需注册该类:

// this is your class exposed to JS 
  Test = class 
    class procedure doit(const v: string); 
  end; 

initialization 
// Register your class 
  TCefRTTIExtension.Register('app', Test);

// and in JavaScript code to call that class above:
app.doit(''foo'')', '', 0); 
酷!不是吗

上述代码摘自:

2010年2月以来推出的一些RTTI介绍:

我在使用DelphiWrapper时遇到了同样的问题

首先,使用{$METHODINFO ON}启用RTTI将防止异常:

引发一个异常,该异常找不到任何名为getObj的属性

按如下方式编写t简单类:

{$METHODINFO ON}
TSimple = Class
Private
  function getvar1:string;
Public    
Published
  property var1:string read getVar1;
  function getObj:TSimple;
end;
{$METHODINFO OFF}
Using MySelf:  <type 'int'> 31957664
Using MySelf2:  <type 'Point'> (2, 5)
现在getObj函数返回一个值——一个整数

我会让你们看看我做了什么。我修改了Demo32:

单元1.pas:

TPoint = class(TPersistent)
private
  fx, fy : Integer;
  fName : String;
public
  constructor Create();
  procedure OffsetBy( dx, dy : integer );
  function MySelf: TPoint;  //**access self using function**
published
  property x : integer read fx write fx;
  property y : integer read fy write fy;
  property Name : string read fName write fName;
  property MySelf2: TPoint read MySelf; //**access self using property**
end;
备忘录1中的Python代码。行:

import spam

p = spam.Point(2, 5)

b = p.MySelf()  //**using function**
print 'Using MySelf: ', type(b), b
c = p.MySelf2   //**using property**
print 'Using MySelf2: ', type(c), c
然后给出如下结果:

{$METHODINFO ON}
TSimple = Class
Private
  function getvar1:string;
Public    
Published
  property var1:string read getVar1;
  function getObj:TSimple;
end;
{$METHODINFO OFF}
Using MySelf:  <type 'int'> 31957664
Using MySelf2:  <type 'Point'> (2, 5)
使用我自己:31957664
使用我自己2:(2,5)
函数返回一个int,这是有线的。可能是指向DelphiWrapper错误包装的TPoint对象的指针


最后,我在Demo8中使用“AddMethod”找到了一种折衷的方法。一点也不漂亮!!但它是有效的。

有人没有说明原因就投了反对票(有些人有一个坏习惯,我希望我能投反对票!)我的猜测是,你应该更多地解释你的意思(可能是一段代码片段),什么不起作用,或者你已经尝试过什么。这个问题对我来说确实很模糊。缺少活动支持了这种感觉。好了,伙计们,看一些代码片段。乍一看确实很奇怪。更简单的方法是否有效,例如返回整数?可能它返回一个指向类本身的指针这一事实使包装器感到困惑——它将如何处理这个问题?如何“使TPySimple像demo32一样为类提供对python代码的访问”?你能链接到他们的文档或显示生成的导入文件中的内容吗(如果是人类可读的,我不知道。)这是一个糟糕的文档组件(),但你可以下载源代码并运行演示,在演示32中,他们向我们展示了如何在python脚本中使用delphi类,我认为TPySimple的代码对我们没有帮助,但是现在这里有很多人。