Estoy usando python4delphi. ¿Cómo puedo devolver un objeto de una función de clase Delphi envuelta?Python4Delphi: Devolver un objeto python en una función. (DelphiWrapper)
Fragmento de código:
Tengo una sencilla clase de Delphi que envolvía al script Python, ¿verdad?
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;
Hice el TPySimple como el demo32 para dar acceso de clase al código de Python. El nombre de mi módulo Python es prueba.
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;
Y ahora el código 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.
Alguien votó sin decir por qué (una mala costumbre que algunas personas tienen, ¡me gustaría poder rechazarlas!) Aunque creo que deberías explicar más sobre lo que quieres decir (tal vez un fragmento de código) y lo que no funciona o lo que ya has intentado. –
Esta pregunta me parece bastante vaga. La falta de actividad respalda ese sentimiento. –
Muy bien muchachos, tomen algunos fragmentos de código. –