Why do I need np.array () or np.copy ()?

The real question about the novelty is zero.

I have a numpy array named 'image'. Performing this action:

image2 = image
image2[image < minval] = minval
image2[image > maxval] = maxval

... changes the contents of the "image".

I understand, because variables in Python are really references, so "image2" is another way to refer to an "image". So I have to use "image2 = np.copy (image)". Good.

But then why does "a" not change when I do this:

a = 5
b = a
b = 7

Isn't 'b' just another way of referring to 'a'? If so, why doesn’t it end at the end == 7?

I want to know if there is some kind of mental model that makes it seem consistent. Because it is not.

+4
1

, b=a b=7. b=a , a, b. b=7 , b, b. , a - (, ) (, numpy). a .

, image2[image < minval] = minval . [], (__setitem__) image2. , image2.

Python "". , , , (, image2 tuple, [] ).

, C/++, , , . , , , : , ( ) , C/++ const.

:

  • : bool, int, long, float complex
  • : str, unicode ( python 2.x) bytes ( python 3.x)
  • tuple ( list)
+4

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


All Articles