- Documentation
- Reference manual
- Packages
- SWI-Prolog Python interface
- library(janus): Call Python from Prolog
- py_version/0
- py_call/1
- py_call/2
- py_call/3
- py_iter/2
- py_iter/3
- py_is_object/1
- py_free/1
- py_with_gil/1
- py_func/3
- py_func/4
- py_dot/4
- py_dot/5
- values/3
- keys/2
- key/2
- items/2
- py_shell/0
- py_pp/1
- py_pp/2
- py_pp/3
- py_obj_dir/2
- py_obj_dict/2
- py_initialize/3
- py_lib_dirs/1
- py_add_lib_dir/1
- py_add_lib_dir/2
- Handling Python errors in Prolog
- Janus and virtual environments (venv)
- library(janus): Call Python from Prolog
- SWI-Prolog Python interface
[det]py_call(+Call)
Arguments to Python functions use the Python conventions. Both
positional and keyword arguments are supported. Keyword
arguments are written as Name = Value and must appear after
the positional arguments.
Below are some examples.
% call a built-in
?- py_call(print("Hello World!\n")).
true.
% call a built-in (alternative)
?- py_call(builtins:print("Hello World!\n")).
true.
% call function in a module
?- py_call(sys:getsizeof([1,2,3]), Size).
Size = 80.
% call function on an attribute of a module
?- py_call(sys:path:append("/home/bob/janus")).
true
% get attribute from a module
?- py_call(sys:path, Path)
Path = ["dir1", "dir2", ...]
Given a class in a file dog.py such as the following
example from the Python documentation
class Dog:
tricks = []
def __init__(self, name):
self.name = name
def add_trick(self, trick):
self.tricks.append(trick)
We can interact with this class as below. Note that $Doc
in the SWI-Prolog toplevel refers to the last toplevel binding for the
variable Dog.
?- py_call(dog:'Dog'("Fido"), Dog).
Dog = <py_Dog>(0x7f095c9d02e0).
?- py_call($Dog:add_trick("roll_over")).
Dog = <py_Dog>(0x7f095c9d02e0).
?- py_call($Dog:tricks, Tricks).
Dog = <py_Dog>(0x7f095c9d02e0),
Tricks = ["roll_over"]
py_call/1 can also be used to
set an attribute on a module or object using the syntax py_call(Obj:Attr = Value).
For example:
?- py_call(dog:'Dog'("Fido"), Dog),
py_call(Dog:owner = "Bob"),
py_call(Doc:owner, Owner).
Dog = <py_Dog>(0x7ffff7112170),
Owner = "Bob".
If the principal term of the first argument is not Target:Func,
The argument is evaluated as the initial target, i.e., it must be an
object reference or a module. For example:
?- py_call(dog:'Dog'("Fido"), Dog),
py_call(Dog, X).
Dog = X, X = <py_Dog>(0x7fa8cbd12050).
?- py_call(sys, S).
S = <py_module>(0x7fa8cd582390).
Options processed:
- py_string_as(+Type)
- If Type is
atom(default), translate a Python String into a Prolog atom. If Type isstring, translate into a Prolog string. Strings are more efficient if they are short lived. - py_object(Boolean)
- If
true(defaultfalse), translate the return as a Python object reference. Some objects are always translated to Prolog, regardless of this flag. These are the Python constantsNone,TrueandFalseas well as instances of the Python base classes long, float, string or tuple. Instances of sub classes of these base classes are controlled by this option.