5 Calling Prolog from Python
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog Python interface
        • Calling Prolog from Python
          • Janus iterator query
          • Janus iterator apply
          • Janus access to Python locals and globals
          • Janus and Prolog truth
          • Janus class Term
          • Janus class PrologError

5.1 Janus iterator query

Class janus.query() is similar to the janus.query_once() function, but it returns a Python iterator that allows for iterating over the answers to a non-deterministic Prolog predicate.

The iterator also implements the Python context manaager protocol (for the Python with statement).

query janus.query(query, inputs={}, keep=False)
As janus.query_once(), returning an iterator that provides an answer dict as janus.query_once() for each answer to query. Answers never have truth False. See discussion above.
Compatibility
PIP. The keep is a SWI-Prolog extension.
Query janus.Query(query, inputs={}, keep=False)
Deprecated. This class was renamed to janus.query(.)
dict|None janus.query.next()
Explicitly ask for the next solution of the iterator. Normally, using the query as an iterator is to be preferred. See discussion above. q.next() is equivalent to next(q) except it returns None if there are no more values instead of raising the StopIteration exception.
None janus.query.close()
Close the query. Closing a query is obligatory. When used as an iterator, the Python destructor (__del__()) takes care of closing the query. However, Python does not guarantee when the destructor will be called, so it is recommended that the context manager protocol is used (with the Python with statement), which closes the query when the query goes out of scope or when an error happens.
Compatibility
PIP.