我正在使用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.发布于 2014-05-15 18:19:29
根据OP,他在这里找到了答案:http://code.google.com/p/python4delphi/issues/detail?id=17
(复制-粘贴以供参考)
嗨,
我有一个建议--通过利用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); 凉爽的!难到不是么?
上面的代码摘自:http://groups.google.com/group/delphichromiumembedded/browse_thread/thread/1793e7ca66012f0c/8ab31a5ecdb6bf48?lnk=gst&q=JavaScript+return+#
自D2010以来引入的一些关于RTTI的介绍:http://robstechcorner.blogspot.com/2009/09/delphi-2010-rtti-basics.html
发布于 2016-09-30 17:36:46
我在使用DelphiWrapper时遇到了同样的问题。
首先,使用{$METHODINFO ON}启用RTTI将防止异常:
引发一个异常,该异常找不到任何名为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“的折衷方法。一点也不漂亮!!但它是有效的。
https://stackoverflow.com/questions/9771261
复制相似问题