Jump to content
IanH9999

vars function within Delphi

Recommended Posts

Hello

 

I've just started using Python4Delphi after watching the excellent videos by Kiriakos and Jim. Huge potential, but with a steep learning curve !

 

I've got the following python code that takes an object and prints it's attributes:

def outputClassInfo(anObject):
    for att_name in vars(anObject):
        anAttr = getattr(anObject,att_name)
        print (att_name,anAttr.__class__)

 

I'm trying to repeat this function in Delphi by adapting demo6 (adding a delphi method to module), but I'm really struggling as I can't find the equivalent of the  "vars" function. Am I going about this the right way? I've trying using the debugger to examine the content of anObject but can't find anything in there either?

 

Thanks for any help...

Ian

 

 

 

Share this post


Link to post

vars is a builtin function.  You can use

 

BuiltinModule.vars to access it using VarPyth

 

If you are on Delphi 10.3 or later you can write something like 

for V in BuiltinModule.vars(anObject) do begin
  anAttr := BuiltInModule.getattr(anObject, V)
  Writeln(V, anAttr.__class__)
end;

assuming anObject is a Custom Variant pointing to the python object.  (out of memory and not tested)

Share this post


Link to post

Hello and thanks for your help. Unfortunately I'm still a bit stuck 😞

 

I'm not sure how the Custom Variant is used. I can't find any example it in the P4D examples. I've also had a good look in the PyScripter source in case it's used there, but I couldn't find usage of it. I did find usage of the BuiltinModule in there which has helped.

 

In my naivety, I was hoping it was going to be something like...

 

function TForm1.spam_foo(pself, args : PPyObject) : PPyObject; cdecl;
var
  v : Variant;
begin
  with GetPythonEngine do
  begin
    for v in BuiltinModule.vars(args) do
    begin
...

 

but it's obviously more complex than this. I assume the function "PyObjectAsVariant" isn't going to help out here is it?

 

I've had a look for info on TCustomVariant as I'm not familiar with it but there doesn't seem to be much written about it. The information in the Embarcadero help file is difficult to relate to using it in this context.

 

(I'm using XE5 in case that makes any difference)

 

Ian

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×