Python tutorials


Although pygmo is available both on Python 2.7 and Python 3, the tutorials (and pygmo’s documentation in general) use the syntax of Python 3. Although many code snippets, examples, etc. will also run on Python 2.7 without modifications, in some cases they will have to be adapted in order to be run successfully on Python 2.7.

A prominent example is the definition of new user-defined classes (such as optimisation problems, algorithms, etc.). Whereas, in Python 3, when defining of a user-defined problem (UDP) we can simply write

class my_problem:

in Python 2.7 we must write instead

class my_problem(object):

(that is, the UDP must derive from object). Failure to do so will result in runtime errors when using the API of problem. In this specific case, the problem lies in the dichotomy between old and new style classes in Python 2.x (see here for an in-depth explanation). pygmo will assume that, like in Python 3, all classes are new-style classes.

Our general recommendation is, if possible, to use Python 3 (or, at least, to use Python 2 styles and idioms that are forward-compatible with Python 3).

Coding your own island (UDI)