From d0af4f50d301cd971e7dadf1e48a5b5c8c66e4b4 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 5 Jul 2013 06:30:16 +0100 Subject: Add %pythonbegin directive. For adding code at the beginning of the generated .py file. --- Doc/Manual/Python.html | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'Doc/Manual') diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html index 3dde20887..6a22738bc 100644 --- a/Doc/Manual/Python.html +++ b/Doc/Manual/Python.html @@ -3314,6 +3314,53 @@ what can be done without having to rely on any of the more advanced customization features.

+

+There is also %pythonbegin which is another directive very similar to %pythoncode, +but generates the given Python code at the beginning of the .py file. +This directive works in the same way as %pythoncode, except the code is copied +just after the SWIG banner (comment) at the top of the file, before any real code. +This provides an opportunity to add your own description in a comment near the top of the file as well +as Python imports that have to appear at the top of the file, such as "from __future__ import" +statements. +

+ +

+The following shows example usage for Python 2.6 to use print as it can in Python 3, that is, as a function instead of a statement: +

+ +
+
+%pythonbegin %{
+# This module provides wrappers to the Whizz Bang library
+%}
+
+%pythonbegin %{
+from __future__ import print_function
+print("Loading", "Whizz", "Bang", sep=' ... ')
+%}
+
+
+ +

+which can be seen when viewing the first few lines of the generated .py file: +

+ +
+
+# This file was automatically generated by SWIG (http://www.swig.org).
+# Version 2.0.11
+#
+# Do not make changes to this file unless you know what you are doing--modify
+# the SWIG interface file instead.
+
+# This module provides wrappers to the Whizz Bang library
+
+from __future__ import print_function
+print("Loading", "Whizz", "Bang", sep=' ... ')
+
+
+
+

Sometimes you may want to replace or modify the wrapper function that SWIG creates in the proxy .py file. The Python module in SWIG provides some features that enable you to do this. First, to -- cgit v1.2.3