Value Exchange Between Python and Prolog¶
String Interpolation from Python to Prolog¶
Currently there’s limited support for converting Python values automatically to Prolog via a string interpolation mechanism.
This mechanism is available to be used with the following Prolog class methods:
assertzassertaretractquery
These methods take one string format argument, and zero or more arguments to replace placeholders with in the format to produce the final string.
Placeholder is %p for all types.
The following types are recognized:
String
Integer
Float
Boolean
pyswip.Atompyswip.VariableLists of the types above
Other types are converted to strings using the str function.
Python Value |
String |
|---|---|
str |
|
int |
|
float |
|
bool |
|
bool |
|
|
|
|
|
list |
|
Other |
|
The placeholders are set using %p.
Example:
ids = [1, 2, 3]
joe = Atom("joe")
Prolog.assertz("user(%p,%p)", joe, ids)
list(Prolog.query("user(%p,IDs)", joe))