Jump to content
Sign in to follow this  


Recommended Posts

Is there easy way to create lambda in Object Pascal and pass it to Python?

Thank you in advance.

Share this post

Link to post


var lambdas : string = 'λλλ';




Jokes aside - what you mean by lambdas? Inline functions?

Share this post

Link to post
1 hour ago, Fr0sT.Brutal said:

Jokes aside - what you mean by lambdas? Inline functions?

I mean python ananymous functions  https://python-reference.readthedocs.io/en/latest/docs/operators/lambda.html with "lambda" keyword syntax

Idea was create callable Python object in Pascal code using some wrapping and then pass it instead of lambda into, say, pandas .apply() method


The question can be somewhat generalized as follows: create an object in Pascal that can be passed to Python as a callable.

While wrapping anonymous method Delphi would be an elegant solution, but it is not necessary


Edited by KoRiF

Share this post

Link to post

No, not quite, but it was your link that helped me unravel the tangle and find the required solution. So thank you very much!!!


My current solution is:

  1. Create field that contains reference to function that will be called a-la lambda later
  2. Create cdecl Delphi method that wraps the call of the "lambda" field with cdecl convention
  3. In the module initialization event handler initialize "lambda" field by a stub and add cdecl Delphi method-wrapper to the module under the name that cannot be just a 'lambda' for some odd reason 
  4. Create method that evaluates added delphi method as python callable object  
  5. Assign "lambda" field with an anonymous Delphi method 
  6. Evaluate added Delphi method that wraps "lambda" as python callable object
  7. Pass evaluated python callable object as python callable parameter
  8. Repeat steps (5) - (7) in other place if necessary


In order to emulate several "lambdas" simultaneously, we would need to maintain something like a dictionary on the python side


And yes, strictly speaking, this is neither a lambda nor on the Delphi side (in Delphi there is no pure concept of a lambda at all, and even a very close "anonymous method" term poorely suited due to, ironically, our callable object is renamed several times here), nor on the python side (it's just a permanent object of a named python function there)


However, I use the term "lambda" because it fits the problem well conceptually: we wanted to define our callable at the call site, and in terms of the top-level algorithm (not the implementation), we achieved this


This mostly works as expected, although for some other use cases I faced with unexpected issues in argument parsing. However, these issues are likely outside the scope of the question.


Memory leaks are still relevant for such "lambda emulation" and this is an important aspect since calls can be repeated thousands of times so need to be careful


Many thanks again!  



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
Sign in to follow this