Specifying Variables in Python

Suppose I create a function A that is used as an argument in another function, so B(A) . Function A points to a record in the array (SciPy) C If I change the array C from B , then the value in the array will be changed globally, so that changes to A will also be noted. Here is an example:

 def pointing_test2(inputs, fs): local = inputs print (local is initial) print fs(0) local[0] += 1.0 print fs(0) initial = sc.array([1.0]) func = lambda x: initial[0] pointing_test2(initial, func) ------- Output ------- True 1.0 2.0 [ 2.] 

You can avoid the fact that the C array is changed globally by making a copy locally inside B , for example:

 [...] local = inputs.copy() [...] ------- Output ------- False 1.0 1.0 [ 1.] 

What I want to achieve halfway! I would like function A point to a copy of the array locally, so inside B ! Thus, I would like to get the following result:

 ------- Output ------- False 1.0 2.0 [ 1.] 

How can i achieve this?

+4
source share
1 answer

The easiest way is to write A so that B passes it an array for operation (hence: locally modified). Is there a reason you don't want to do this?

 def func(arr): return arr[0] 

(By the way, your func ignores its argument).

0
source

Source: https://habr.com/ru/post/1401490/


All Articles