1

I am using Inline::Python 0.56, trying to call a Python function with named parameters from Perl. I have replaced the original code fragments with an example that can be run, as requested:

use strict;
use warnings;
use 5.16.3;

use Inline Python => <<'END_OF_PYTHON_CODE';
# Interface
def get_sbom_of_package (
    package_name = None,
    target_path  = None,
):
    assert package_name is not None
    assert target_path is not None

    print ( package_name, target_path )
    return package_name

END_OF_PYTHON_CODE

my $package = 'thePackage';
my $target_path  = $ENV{HOME};

my $sbp = get_sbom_of_package(
    package_name => $package ,
    target_path  => $target_path
);
say $sbp;

and my error is:

TypeError: get_sbom_of_package() takes from 0 to 2 positional arguments but 8 were given at (eval 3) line 3.

Is there something else I need to do the inline Python to understand I am feeding it named parameters? I have replaced the call in Perl with

my $sbp = get_sbom_of_package(
    $package ,
    $target_path
);

and it works just fine. I am thinking it's either

  • bad Python on my part or
  • an Inline::Python configuration issue
  • an Inline::Python limitation

sorted from most likely to least. :-)

8
  • It would be better if you could publish an isolated test case we can run. Commented Sep 14, 2022 at 19:10
  • Also when you say "Inline::Perl 0.56" you mean "Inline::Python"? Commented Sep 14, 2022 at 19:11
  • Thank you for correcting the question. I have replaced the fragments with a runnable. Commented Sep 14, 2022 at 20:08
  • I don't see any examples of keyword arguments for Python functions in any of the documentation. From the error message, it sounds like the named arguments in the Perl function call are just translated into a sequence of positional arguments, alternating keys and values. Commented Sep 14, 2022 at 20:40
  • Thanks for updating. Please correct me if I'm goofing off here, but: your Perl function call receives 4 (four) arguments, your Python function is written to take 2 (each with a default value) ... it disagrees, no? Commented Sep 14, 2022 at 20:58

3 Answers 3

2

Turns out it is not supported: https://rt.cpan.org/Public/Bug/Display.html?id=91360

Sign up to request clarification or add additional context in comments.

20 Comments

That is not "unsupported" -- in Perl, a call func(a => 1) passes two arguments to the subroutine func. It's just how the language is. The expression in parens is evaluated to a list of scalars and then that's passed to the sub. (Just a comment on the "Bug," which I don't think is a bug -- not a criticism of your post :)
So, I don't see how one would implement Python's call with named arguments (func(a=7)) on the Perl side ...? Anything that comes to mind (for me) is an existing Perl syntax meaning something else. Perhaps one could say that that feature isn't implemented, as it would require new syntax on Perl side?
@zdim did you even read the rt issue? the author answers your question py_call_function_named("package", "function", \@positional, \%named) function, but I'm not sure it's worth the effort."_ Ie., named arguments could provided in a separate named hr, and then simply passed to the python api. Which is how python itself actually works, it's just sugar.
@EvanCarroll "Put it another way,..." OK, I see what you mean ... that's the opposite direction from what I was thinking. I take it that I have a wanted API in Perl and am writting code in Python for it; a "normal" way to develop libraries I'd say. But, of course, one could say "I want to port this great Python code to Perl"... then the "bug" makes some sense (even though one can't really expect to implement language features word by word)
hah :) (you mean he had some Python code he couldn't change and was calling it from Perl the way he shows in that rt?) ok, that makes sense then... (it's not at all what this question asks though but that's ok)
|
1

If I am reading the XS correctly and remembering my Python extension module work, this is not directly possible.

Inline::Python appears to invoke py callables using PyObject_CallObject which passes a tuple of arguments — without any named/keyword args.

So, you'd have to do it yourself, perhaps by putting a wrapper around the py callable, which wrapper understands what you mean by parameter => value, ... and constructs the right tuple to pass along. You'd need to redundantly know default values, too, of course. You might rev Inline::Python to support interrogating the callable's signature to look for keyword args ... but then you'd still have to adapt those semantics that to ordinary Perl subroutine calls yourself.

5 Comments

Interesting. I'll go look at the code you referenced. I wonder, though; that seems to imply that if the function I was calling in Python required named params, then I couldn't call it directly. From my research, I think that Python functions can be called with named or positional commands, so there is nothing that can be interrogated.
It's little more complicated. "Regular" parameters can be set using positional or keyword arguments. Python also has positional-only and keyword-only parameters, which (as their names imply) can only be set using positional and keyword arguments, respectively. Such a function could be called using this syntax: foo(**{name: value, name: value}), so perhaps in Perl you could pass a hash ref, like foo({name => value, name => value})?
(Though that looks like Inline::Python would have to use PyObject_Call, not PyObject_CallObject. I wonder why they didn't.)
I tried that hash ref idea. Didn't work, probably for the reason you suggested.
Looks like the package predates keyword-only parameters, and the author assumed keyword arguments would never be needed.
1

Note   This is meant to sketch the idea that "named arguments" be implemented as a hash(ref) in Perl --> dictionary in Python, and then do whatever need be done on the Python side, with named arguments in hand. By all means add other needed arguments to the list (or to the dictionary), like function names, arrayrefs, or whatnot.

The ultimate purpose isn't explained but if that is clarified I can add more specific code. (A guess at how this might be used, plus another fairly generic way, are given in a footnote.)


The attempted code passes four (4) arguments to Perl's function call, while Python's function is written to take two (each with a default value).

If you want to pass named parameters to a Python function, one way would be to pass a dictionary. Then this incidentally mimics your Perl use, as well

def adict(d):
    for key in d:
        print("key:", key, "value:", d[key])

This way one maps named arguments from the caller in Perl (hash) to Python (dictionary), leaving it to normal python->python calls to be built in this wrapper.

Then your code could go as

use warnings;
use strict;
use feature 'say';

use Inline::Python;

my $package = 'thePackage';
my $target_path  = $ENV{HOME};

call_py( {  # note -- pass a hashref
    package_name => $package, target_path  => $target_path
} );

use Inline Python => <<'END_PY'
def call_py(d):
    for key in d:
        print(key + " => " + d[key])
    # ...
    # prepare and make other (python-to-python) calls as needed
END_PY

If specific further calls from the Python function need be made (by named parameters?) please show how they'd look and I can add to this bare-bones example.

See below on my guesses of what could be intended with this question.

This prints (with my path redacted)

target_path => /...
package_name => thePackage

Or one could devise a system for passing Perl's four arguments and taking them in Python and interpreting them as name => value pairs, but why.

Once arguments in the Python function are unpacked from the dictionary, along with whatever else may have been added to the argument list along with the dictionary for named arguments sent from Perl via a hash(ref), one can make further python -> python calls as needed.


A function in Perl takes a list of scalars. Period. So this

func(package_name => $package, target_path  => $target_path)

or even this

my %ph = (package_name => $package, target_path  => $target_path);

func( %ph );

is really this

func('package_name', $package, 'target_path', $target_path)

That "fat comma" (=>) is a comma, whereby its left argument also gets quoted.


Imagine that the purpose of this is to be able to call from Perl a great py_lib in Python, somehow via "named parameters" (please clarify). Here are a couple of guesses

use warnings;
use strict;
use feature 'say';

use Inline::Python;

my $package = 'thePackage';
my $target_path  = $ENV{HOME};

call_py({ package_name => $package, target_path => $target_path });

use Inline Python => <<'END_PY'
def call_py(d):
    print(d)
    # prepare and make other (python-to-python) calls as needed

    print("\nArgs with these names passed from Perl:")
    py_lib_kw( pack=d['package_name'], path=d['target_path'] )

    print("\nVariable number of named args passed from Perl:")
    py_lib_kwargs( **d )

def py_lib_kw(path=None, pack=None):
   print ("package is " + pack + " and path is " + path)

def py_lib_kwargs(**data):
    for val in data:
        print(val + " => " + data[val])

END_PY

18 Comments

@ikegami "That wasn't the question." -- how was that not the question? They want a function that works with named parameters; that's a dictionary -- I'm suggesting to them to implement it as a dictionary (on Python side). I don't see that their wording of "named parameters" means what we think, judged by their example. (Has nothing to do with Python's named parameters, just a function with two arguments.) I am trying to read the actual question and it wants a function with parameters.
@ikegami "call def get_sbom_of_package( package_name = None, target_path = None ): using names, which is what the OP wants. Simple as that." -- not simple: 1) I don't see that they ask that. I see that they want a Perl function to pass named arguments and a Python function for it, that uses those named args, and they wrote something they thought might work. I don't see that the shown Python function is the requirement? 2) But even if they want what you say, I am clearly suggesting they write it differently, with a dictionary, and then they can call using names. never mind...
@ikegami "The wrapper doesn't have to take the function name. Makes it reusable, though. It would be silly to have a copy the same wrapper with a different hardcoded name for each function" --- wait, so you are saying they want to write a wrapper to use some yet other Python function (a great library or some such), so that's the function name to pass? But the question doesn't even hint at anything like it!? Yes, it's a perhaps the most frequent use, but it doesn't ask that! And the rest of it ... you say the dictionary, just what I'm proposing here! whatever. i'm outta here
@ikegami "I was just trying to help you salvage you answer" -- well thank you, I thought so at first, but I don't see how. you mention a function name and a dictionary? I did pass a hashref -> dictionary? so they can add a function name and their python function can call that target library with parameters unpacked from the dictionary. Called out of Perl via named args (hash). Anything else? This is even more of an answer if they are writing merely a wrapper (Add "func_name": name to that dictionary for an even simpler interface. Heck, add all needed to that one hashref->dictionary)
@ikegami I propose to pass a hash(ref) into a dictionary. you say 'no that's not what they are asking', instead 'write a Python function that accepts a function name and a dictionary' (paraphrasing) ...? I don't get it. Evan-Carrol below talks about the author's comment, in a linked rt issue, about how this can be implemented using ... a dictionary, guess what! (And then some, sure, but "named" goes by dict) While I get crap (and a downvote for good measure I guess). I don't understand. But clearly something isn't matching here so I I'll just leave it then. Sorry for the bother...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.