@@ -28,9 +28,9 @@ Depending on the situation, these steps can be carried out in a single composite
2828command or step-by-step; in which case some steps can be omitted or combined
2929with others.
3030
31- Below we describe three typical approaches of using F2PY. These can be read in
31+ Below, we describe three typical approaches of using F2PY. These can be read in
3232order of increasing effort, but also cater to different access levels depending
33- on whether the Fortran code can be modified freely.
33+ on whether the Fortran code can be freely modified .
3434
3535The following example Fortran 77 code will be used for
3636illustration, save it as ``fib1.f ``:
@@ -42,17 +42,17 @@ illustration, save it as ``fib1.f``:
4242The quick way
4343==============
4444
45- The quickest way to expose the Fortran subroutine ``FIB `` to Python is
46- to run
45+ The quickest way to wrap the Fortran subroutine ``FIB `` for use in Python is to
46+ run
4747
4848::
4949
5050 python -m numpy.f2py -c fib1.f -m fib1
5151
52- This command builds (see the ``-c `` flag, or execute `` python -m numpy.f2py ``
53- without arguments to see the explanation of command line options) an extension
54- module `` fib1.so `` (see the `` -m `` flag) to the current directory. Now, in Python
55- the Fortran subroutine ``FIB `` is accessible via ``fib1.fib ``::
52+ This command compiles and wraps ``fib1.f `` (`` -c ``) to create the extension
53+ module `` fib1.so `` (`` -m ``) in the current directory. A list of command line
54+ options can be seen by executing `` python -m numpy.f2py ``. Now, in Python the
55+ Fortran subroutine ``FIB `` is accessible via ``fib1.fib ``::
5656
5757 >>> import numpy as np
5858 >>> import fib1
@@ -116,9 +116,8 @@ the Fortran subroutine ``FIB`` is accessible via ``fib1.fib``::
116116 >>> print(a)
117117 [1 1 1 1 1 1 1 1]
118118
119- Clearly, this is not an expected behaviour; especially since Fortran
120- typically passes by reference. The fact that the above example worked with
121- ``dtype=float `` is considered accidental.
119+ Clearly, this is unexpected, as Fortran typically passes by reference. That
120+ the above example worked with ``dtype=float `` is considered accidental.
122121
123122 F2PY provides an ``intent(inplace) `` attribute that modifies
124123 the attributes of an input array so that any changes made by
@@ -131,30 +130,30 @@ the Fortran subroutine ``FIB`` is accessible via ``fib1.fib``::
131130 >>> print(a)
132131 [ 0. 1. 1. 2. 3. 5. 8. 13.]
133132
134- However, the recommended way to get changes made by Fortran subroutine
135- propagate to Python is to use ``intent(out) `` attribute. This approach is
133+ However, the recommended way to have changes made by Fortran subroutine
134+ propagate to Python is to use the ``intent(out) `` attribute. That approach is
136135 more efficient and also cleaner.
137136
138137 * The usage of ``fib1.fib `` in Python is very similar to using ``FIB `` in
139- Fortran. However, using *in situ * output arguments in Python indicates a
140- poor style as there are no safety mechanisms in Python to protect against
141- wrong argument types. When using Fortran or C, compilers naturally discover
142- any type mismatches during the compilation process, but in Python the types
143- must be checked at runtime. So , using *in situ * output arguments in Python
144- may give rise to difficult to find bugs, not to mention the fact that the
138+ Fortran. However, using *in situ * output arguments in Python is poor style,
139+ as there are no safety mechanisms in Python to protect against wrong
140+ argument types. When using Fortran or C, compilers discover any type
141+ mismatches during the compilation process, but in Python the types must be
142+ checked at runtime. Consequently , using *in situ * output arguments in Python
143+ may lead to difficult to find bugs, not to mention the fact that the
145144 codes will be less readable when all required type checks are implemented.
146145
147- Though the approach to exposing Fortran routines to Python discussed so far is
146+ Though the approach to wrapping Fortran routines for Python discussed so far is
148147 very straightforward, it has several drawbacks (see the comments above).
149- These drawbacks are due to the fact that there is no way for F2PY to determine
148+ The drawbacks are due to the fact that there is no way for F2PY to determine
150149 the actual intention of the arguments; that is there is ambiguity in
151- distinguishing between input, output arguments. So , F2PY conservatively
152- assumes that all arguments are input arguments by default.
150+ distinguishing between input and output arguments. Consequently , F2PY assumes
151+ that all arguments are input arguments by default.
153152
154- However, there are ways (see below) how to remove this ambiguity; essentially
155- by "teaching" F2PY about the true intentions (among other things) of function
156- arguments; subsequently F2PY is then able to generate more Pythonic (more
157- explicit, easier to use, and less error prone) wrappers to Fortran functions.
153+ However, there are ways (see below) to remove this ambiguity by "teaching"
154+ F2PY about the true intentions of function arguments, and F2PY is then able to
155+ generate more explicit, easier to use, and less error prone wrappers for
156+ Fortran functions.
158157
159158The smart way
160159==============
@@ -175,13 +174,13 @@ one.
175174 :language: fortran
176175
177176* Next, we'll teach F2PY that the argument ``n `` is an input argument (using the
178- ``intent(in) `` attribute) and that the result, i.e. the contents of ``a ``
179- after calling Fortran function ``FIB ``, should be returned to Python (using
177+ ``intent(in) `` attribute) and that the result, i.e., the contents of ``a ``
178+ after calling the Fortran function ``FIB ``, should be returned to Python (using
180179 the ``intent(out) `` attribute). In addition, an array ``a `` should be created
181180 dynamically using the size determined by the input argument ``n `` (using the
182181 ``depend(n) `` attribute to indicate this dependence relation).
183182
184- The content of a suitably modified version of ``fib1.pyf `` (saved as
183+ The contents of a suitably modified version of ``fib1.pyf `` (saved as
185184 ``fib2.pyf ``) is as follows:
186185
187186 .. literalinclude :: ./code/fib2.pyf
@@ -214,13 +213,12 @@ In Python::
214213
215214.. note ::
216215
217- * Clearly, the signature of ``fib2.fib `` now more closely corresponds to the
216+ * The signature of ``fib2.fib `` now more closely corresponds to the
218217 intention of Fortran subroutine ``FIB ``: given the number ``n ``,
219218 ``fib2.fib `` returns the first ``n `` Fibonacci numbers as a NumPy array.
220- Also, the new Python signature ``fib2.fib `` rules out the surprises that we
221- experienced with ``fib1.fib ``.
219+ The new Python signature ``fib2.fib `` also rules out the unexpected behaviour in ``fib1.fib ``.
222220
223- * Note that by default using a single ``intent(out) `` also implies
221+ * Note that by default, using a single ``intent(out) `` also implies
224222 ``intent(hide) ``. Arguments that have the ``intent(hide) `` attribute
225223 specified will not be listed in the argument list of a wrapper function.
226224
@@ -233,11 +231,11 @@ modifications to their source codes are not desirable nor even
233231possible.
234232
235233However, if editing Fortran codes is acceptable, then the generation of an
236- intermediate signature file can be skipped in most cases. Namely, F2PY specific
237- attributes can be inserted directly to Fortran source codes using the so-called
238- F2PY directive . A F2PY directive consists of special comment lines (starting
239- with ``Cf2py `` or ``!f2py ``, for example) which are ignored by Fortran compilers
240- but interpreted by F2PY as normal lines.
234+ intermediate signature file can be skipped in most cases. F2PY specific
235+ attributes can be inserted directly into Fortran source codes using F2PY
236+ directives . A F2PY directive consists of special comment lines (starting with
237+ ``Cf2py `` or ``!f2py ``, for example) which are ignored by Fortran compilers but
238+ interpreted by F2PY as normal lines.
241239
242240Consider a modified version of the previous Fortran code with F2PY directives,
243241saved as ``fib3.f ``:
0 commit comments