diff --git a/ag-common/functiontools/pythonfunction.hh b/ag-common/functiontools/pythonfunction.hh
index 9462b6cc2701663d085a3f30dbfa435c1307f5c8..60498dcd0e46ea6f037e5847e86448da20bdbdf8 100644
--- a/ag-common/functiontools/pythonfunction.hh
+++ b/ag-common/functiontools/pythonfunction.hh
@@ -184,13 +184,19 @@ class PythonFunction :
         /**
          * \brief Create PythonFunction from string expression.
          *
-         * This creates a python function with the given name.
+         * This creates a python function with the given name. If expression contains no newline
+         * it is directly evaluated and returned. If it contains a newline it is used as function
+         * body and expected to call return itself.
          */
         PythonFunction(const std::string name, const std::string expression)
         {
             // create a python function
             std::stringstream s;
-            s << "def " << name << "(x,i=0):\n    return " << expression;
+            std::string::size_type newline = expression.find("\n");
+            if (newline != std::string::npos)
+                s << "def " << name << "(x,i=0):\n" << expression;
+            else
+                s << "def " << name << "(x,i=0):\n    return " << expression;
             PyRun_SimpleString(s.str().c_str());
 
             // get reference for python function