0

I have written the below codes to make 2 functions. The only difference is the second line.(input_array *= 7 and input_array = input_array * 7)

But they gave completely different results. (The results are in the picture attached) I wonder why is that and what's the diefference between the two second lines?

def array_times_seven(input_array):
    input_array *= 7

    return input_array

test_array = np.ones((5,5))

array_times_seven(test_array[3:,3:])

test_array

#-----------------------------------------------

def array_times_seven(input_array):
    input_array = input_array * 7

    return input_array

test_array = np.ones((5,5))

array_times_seven(test_array[3:,3:])

test_array

Result for the 1st code

result for the 2nd code

  • 1
    In your first example your function returns nothing. – Reedinationer Feb 28 '20 at 23:53
  • The first mutates the input argument, so you see changes in both the return value (which you ignore) and the global variable. The second assigns a new value to `input_array`, so you only see the changes in the return value. So there's the issue of mutating array, but also the issue of what happens to an input variable when a new object is assigned to it. The second applies to all arguments in a function. – hpaulj Feb 29 '20 at 01:47
  • Pay attention to what the 2 functions return. And test the two actions outside of functions. That should help separate several issues. – hpaulj Feb 29 '20 at 02:03

0 Answers0