Home Online Manual
Top
Back: polytope
Forward: pyobject declarations
FastBack: Data types
FastForward: countedref
Up: Data types
Top: Singular Manual
Contents: Table of Contents
Index: Index
About: About this document

4.25 pyobject

The pyobject is a black box data type in SINGULAR for handling objects from the programming language python. It needs the python support of SINGULAR to be installed.

Together with some basic operations and funtions, pyobject instances access python functionality from within SINGULAR and store the results for re-use:

Note that this feature is automatically loaded on demand when initializing an object of type pyobject. For accessing pyobject-related functions before using any python object, please type LIB("pyobject.so"); at the SINGULAR prompt.

 
pyobject pystr = "Hello";
pyobject pyint = 2;
string singstr = string(pystr + " World!");
singstr;
==> 'Hello World!'
pystr + pyint;  // Error: not possible
==>    ? pyobject error occurred
==>    ? cannot concatenate 'str' and 'int' objects
==>    ? error occurred in or before ./examples/pyobject.sing line 5: `pystr \
   + pyint;  // Error: not possible`
pystr * pyint;  // But this is allowed,
==> 'HelloHello'
pystr * 3;      // as well as this;
==> 'HelloHelloHello'

python_run("def newfunc(*args): return list(args)");  // syncs contexts!
newfunc(1, 2, 3);          // newfunc also knowd to SINGULAR
==> [1, 2, 3]

def pylst = python_eval("[3, 7, 1]");
proc(attrib(pylst, "sort"))(); // Access python member routines as attributes
pylst.sort();             // <- equivalent short-notation
pylst."sort"();           // <- alternative short-notation
pylst;
==> [1, 3, 7]

python_import("os");       // Gets stuff from python module 'os'
name;                      // The identifier of the operating system
==> 'posix'

4.25.1 pyobject declarations  
4.25.2 pyobject expressions  
4.25.3 pyobject operations  
4.25.4 pyobject related functions