[pythran] Re: Pass By Reference For Module Level Variables

  • From: Alex Zhang <azhang82@xxxxxxxxx>
  • To: Pythran <pythran@xxxxxxxxxxxxx>
  • Date: Thu, 4 Apr 2019 17:41:38 -0700

Sorry, I don't know why my first email's format got so messed up. I am
reformatting below:

Hey Serge & Everybody,

First of all, Pythran has been extremely helpful to my projects at work. I
am very grateful.

In my limited understanding, typically pythran’s functions take arguments
by value. But with numpy arrays, it is by reference. However, I am
struggling with how to make it work for module-level variables. Let’s take
a look at the simple example below (saved as test.py):


# pythran export add_np_array(float[:])
# pythran export add_np_array_internal()
# pythran export get_vv()

import numpy as np

def add_np_array(v):

    res = np.sum(v)
    v[0] = v[0] + 10
    return res

vv = np.array([0.0, 0.0, 0.0])

def add_np_array_internal():

    add_np_array(vv)
    add_np_array(vv)
    return vv

def get_vv():
    return vv


After compiling and import test in python, I can run the code below:


import numpy as np
import test
x = np.array([0.0, 0.0, 0.0])
test.add_np_array(x)
0.0

test.add_np_array(x)
10.0

x
array([20.,  0.,  0.]) #So far this is expected.

test.get_vv()
array([0., 0., 0.])

test.add_np_array_internal() #this is very puzzling to me.
array([0., 0., 0.])

test.get_vv()
array([0., 0., 0.])


It is very puzzling to me why the call to add_np_array_internal return [0,
0, 0]. Even if add_np_array_internal is working a local copy of vv,
shouldn’t it pass by reference to add_np_array?

Hope you can help me understand. Also, is it possible to let vv used by
reference (in other words, in the last call of test.get_vv(), I get a
modified value)?

Thank you very much!

On Thu, Apr 4, 2019 at 2:27 PM Alex Zhang <azhang82@xxxxxxxxx> wrote:


Hey Serge & Everybody,



First of all, Pythran has been extremely helpful to my projects at work. I
am very grateful.



In my limited understanding, typically pythran’s functions take arguments
by value. But with numpy arrays, it is by reference. However, I am
struggling with how to make it work for module-level variables. Let’s take
a look at the simple example below (saved as test.py):



# pythran export add_np_array(float[:])

# pythran export add_np_array_internal()

# pythran export get_vv()



import numpy as np



def add_np_array(v):

    res = np.sum(v)

    v[0] = v[0] + 10

    return res



vv = np.array([0.0, 0.0, 0.0])



def add_np_array_internal():



    add_np_array(vv)

    add_np_array(vv)



    return vv



def get_vv():

    return vv



After compiling and import test in python, I can run the code below:



import numpy as np

import test

x = np.array([0.0, 0.0, 0.0])

test.add_np_array(x)

0.0

test.add_np_array(x)

10.0

x

array([20.,  0.,  0.]) #So far this is expected.

test.get_vv()

array([0., 0., 0.])

test.add_np_array_internal() #this is very puzzling to me.

array([0., 0., 0.])

test.get_vv()

array([0., 0., 0.])





It is very puzzling to me why the call to add_np_array_internal return [0,
0, 0]. Even if add_np_array_internal is working a local copy of vv,
shouldn’t it pass by reference to add_np_array?



Hope you can help me understand. Also, is it possible to let vv used by
reference (in other words, in the last call of test.get_vv(), I get a
modified value)?



Thank you very much!



   - Alex


This message and any attachment are confidential and may be privileged or
otherwise protected from disclosure. If you are not the intended recipient,
please telephone or email the sender and delete this message and any
attachment from your system. If you are not the intended recipient you must
not copy this message or attachment or disclose the contents to any other
person.


Other related posts: