KoRiF 1 Posted March 27, 2023 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
Fr0sT.Brutal 900 Posted March 28, 2023 Sure var lambdas : string = 'λλλ'; python.someFunc(PChar(lambdas)); 🙂 Jokes aside - what you mean by lambdas? Inline functions? Share this post Link to post
KoRiF 1 Posted March 28, 2023 (edited) 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 March 28, 2023 by KoRiF Share this post Link to post
Fr0sT.Brutal 900 Posted March 29, 2023 Is this what you're looking for? https://blogs.embarcadero.com/learn-about-using-delphi-methods-as-python-functions-with-python4delphi-sample-app/ Share this post Link to post
KoRiF 1 Posted May 3, 2023 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: Create field that contains reference to function that will be called a-la lambda later Create cdecl Delphi method that wraps the call of the "lambda" field with cdecl convention 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 Create method that evaluates added delphi method as python callable object Assign "lambda" field with an anonymous Delphi method Evaluate added Delphi method that wraps "lambda" as python callable object Pass evaluated python callable object as python callable parameter 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