[SciPy-user] returning an array from weave inline

Emanuele Olivetti olivetti at itc.it
Thu Mar 29 03:45:24 EDT 2007


Flavio Coelho wrote:
> Thanks Fernando,
> 
> I thought variables converted to C were copies, so that there could not be
> an in place operation on a Python variable.

They are copies like it is in the C way of thinking: if you pass a
basic type (e.g. an int) to the inlined C code and modify it then
when you are back to python there is no change in the initial variable
you passed since just the copy was modified. If you pass something more complex
(e.g. a numpy array) then you are just giving a copy of the it's reference
(or pointer) to that function. With that reference (or copy of) you
can access the the _original_ array and modify it. So when you are back to
python your array _is_ changed. This mechanism allows better efficiency:
what happens if you have a huge 2Gb array and pass it to the inline code?
It's not desirable to make a whole copy and allocate other 2Gb or RAM...

Anyway python itself works like that. E.g.:
----
def f(a,b):
     a=a+1
     b.append(1)
     return

a=1
b=[3,2]
print a,b
f(a,b)
print a,b
----

Hope this helps,

Emanuele

------------------
ITC -> dall'1 marzo 2007 Fondazione Bruno Kessler
ITC -> since 1 March 2007 Fondazione Bruno Kessler
------------------



More information about the SciPy-User mailing list