delphi - Python4Delphi:在函数中返回 python 对象。 (德尔福包装器)

标签 delphi delphi-xe2 python4delphi

我正在使用 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
  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.

最佳答案

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

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

raise a exception that not find any attributes named getObj.

像这样编写 TSimple 类:

{$METHODINFO ON}
TSimple = Class
Private
  function getvar1:string;
Public    
Published
  property var1:string read getVar1;
  function getObj:TSimple;
end;
{$METHODINFO OFF}

现在 getObj 函数返回一个值——一个整数!

我会向你们展示我做了什么。我修改了Demo32:

Unit1.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;

Memo1.Lines 中的 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

然后它给出这样的结果:

Using MySelf:  <type 'int'> 31957664
Using MySelf2:  <type 'Point'> (2, 5)

该函数返回一个 int,它是连线的。也许它是一个指向由 DelphiWrapper 错误包装的 TPoint 对象的指针。

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

关于delphi - Python4Delphi:在函数中返回 python 对象。 (德尔福包装器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9771261/

相关文章:

delphi - 防止类型转换可以揭示隐藏的属性?

delphi - 如何在Delphi XE7中获得变体点后的功能和属性建议?

delphi - 如何在Delphi XE2中制作Ribbon画廊?

Python4Delphi驱动的程序,如何部署?

delphi - 如何使用 Python4Delphi 从 Delphi 返回列表到 Python

用于 MIDI 输入/输出的 Delphi 库?

delphi - 单独的数据结构与 VirtualStringTree 的 PVirtualNodes 来存储数据?

mysql - Delphi Rave Report 生成空白页

delphi - 从远程 FTP 下载文件列表

python - 使用 Delphi 创建 Python 扩展