## 4.28 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 functions, 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";
    ==> no pyobject support
    pyobject pyint = 2;
    string singstr = string(pystr + " World!");
    ==>    ? `pyobject` + `string` failed
    ==>    ? expected `string` + `string`
    ==>    ? expected `ring` + `string`
    ==>    ? error occurred in or before ./examples/pyobject.sing line 3: `string\
        singstr = string(pystr + " World!");`
    ==>    ? expected string-expression. type 'help string;'
    singstr;
    ==>    ? `singstr` is undefined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 4: `singst\
       r;`
    pystr + pyint;  // Error: not possible
    ==>    ? `pyobject` + `pyobject` failed
    ==>    ? error occurred in or before ./examples/pyobject.sing line 5: `pystr \
       + pyint;  // Error: not possible`
    pystr * pyint;  // But this is allowed,
    ==>    ? `pyobject` * `pyobject` failed
    ==>    ? error occurred in or before ./examples/pyobject.sing line 6: `pystr \
       * pyint;  // But this is allowed,`
    pystr * 3;      // as well as this;
    ==>    ? `pyobject` * `int` failed
    ==>    ? expected `int` * `int`
    ==>    ? expected `matrix` * `int`
    ==>    ? expected `bigintmat` * `int`
    ==>    ? expected `bigintvec` * `int`
    ==>    ? expected `intvec` * `int`
    ==>    ? expected `intmat` * `int`
    ==>    ? error occurred in or before ./examples/pyobject.sing line 7: `pystr \
       * 3;      // as well as this;`

    python_run("def newfunc(*args): return list(args)");  // syncs contexts!
    ==>    ? `python_run` is not defined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 9: `python\
       _run("def newfunc(*args): return list(args)");  // syncs contexts!`
    newfunc(1, 2, 3);          // newfunc also knowd to SINGULAR
    ==>    ? `newfunc(1,2,3)` is undefined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 10: `newfu\
       nc(1, 2, 3);          // newfunc also knowd to SINGULAR`

    def pylst = python_eval("[3, 7, 1]");
    ==>    ? `python_eval` is not defined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 12: `def p\
       ylst = python_eval("[3, 7, 1]");`
    proc(attrib(pylst, "sort"))(); // Access python member routines as attributes
    ==>    ? proc(`string`) failed
    ==>    ? expected proc(`proc`)
    ==>    ? error occurred in or before ./examples/pyobject.sing line 13: `proc(\
       attrib(pylst, "sort"))(); // Access python member routines as attributes`
    ==>    ? wrong type declaration. type 'help proc;'
    pylst.sort();             // <- equivalent short-notation
    ==>    ? `pylst` is not defined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 14: `pylst\
       .sort();             // <- equivalent short-notation`
    pylst."sort"();           // <- alternative short-notation
    ==>    ? `pylst` is not defined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 15: `pylst\
       ."sort"();           // <- alternative short-notation`
    pylst;
    ==>    ? `pylst` is undefined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 16: `pylst\
       ;`

    python_import("os");       // Gets stuff from python module 'os'
    ==>    ? `python_import` is not defined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 18: `pytho\
       n_import("os");       // Gets stuff from python module 'os'`
    name;                      // The identifier of the operating system
    ==>    ? `name` is undefined
    ==>    ? error occurred in or before ./examples/pyobject.sing line 19: `name;\
                             // The identifier of the operating system`

> 4.28.1 pyobject declarations |  |
> 4.28.2 pyobject expressions |  |
> 4.28.3 pyobject operations |  |
> 4.28.4 pyobject related functions |  |

User manual for Singular version 4.4.1, 2025, generated by texi2html.