简体   繁体   English

为什么我需要np.array()或np.copy()?

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

A real numpy newbie question here. 这是一个真正的numpy新手问题。

I have an numpy array called 'image'. 我有一个名为'image'的numpy数组。 Doing this: 这样做:

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

...changes the contents of 'image'. ...更改“图片”的内容。

I gather that's because variables in Python are really references, so 'image2' is just another way of referring to 'image'. 我收集的是因为Python中的变量确实是引用,所以'image2'只是引用'image'的另一种方式。 So I'm supposed to use "image2 = np.copy(image)". 所以我应该使用“image2 = np.copy(image)”。 Fine. 精细。

But, then, why doesn't 'a' change when I do this: 但是,那么,当我这样做时,为什么不“改变”:

a = 5
b = a
b = 7

Isn't 'b' just another way of referring to 'a'? 不是'b'只是提到'a'的另一种方式吗? If so, why doesn't a==7 at the end of this? 如果是这样,为什么a = = 7在这结束?

I want to know if there's some mental model that makes this seem consistent. 我想知道是否有一些心理模型使这看起来一致。 Because it doesn't. 因为它没有。

The answer really lies in the way direct assignments like b=a and b=7 work. 答案实际上在于直接分配b=ab=7 b=a creates a new reference to the object also referenced by a , and associates that new reference with the name b . b=a创建对也由a引用的对象的新引用,并将该新引用与名称b相关联。 The subsequent b=7 then removes the reference that was attached to the name b , and makes a different association with the name b . 随后b=7 ,然后移除了一个连接到名称引用b ,并与名称的不同的关联b This will be true whether a is an immutable type (like an integer) or a mutable type (like a numpy array). 无论a是不可变类型(如整数)还是可变类型(如numpy数组),都是如此。 In neither case will the content of a be modified. 在任何情况下都不会修改a的内容。

By contrast, image2[image < minval] = minval is not a reassignment. 相比之下, image2[image < minval] = minval不是重新分配。 Via its use of [] it calls a method ( __setitem__ ) of the object image2 . 通过使用[]它调用对象image2的方法( __setitem__ )。 This method changes parts of the underlying data structure without reassigning anything to image2 . 此方法更改基础数据结构的某些部分,而不将任何内容重新分配给image2

Python's most fundamental types are "immutable". Python最基本的类型是“不可变的”。 This means that nothing you can do will change them (for example, if image2 were of the immutable type tuple , trying to change one of its elements with [] indexing would cause an exception to be raised). 这意味着您无法做任何事情都会改变它们(例如,如果image2属于不可变类型tuple ,尝试使用[]索引更改其中一个元素将导致引发异常)。

As a result, very very loosely , if you're accustomed to a C/C++ mindset, it can sometimes help to think of immutable types as being passed by value and mutable types being passed by reference when you're looking at a function or method prototype. 因此, 非常非常松散 ,如果您习惯于C / C ++思维模式,它有时可以帮助将不可变类型视为通过值传递,并且当您查看函数时通过引用传递可变类型或方法原型。 As pointed out in the comments, though, this is not really what happens: everything is a reference, but some references (the immutable types) are automatically treated the way C/C++ would treat const references. 正如评论中指出的那样,这并不是真正发生的事情:一切都是引用,但是一些引用(不可变类型)会按照C / C ++处理const引用的方式自动处理。

The immutable types include: 不可变类型包括:

  • basic numeric types: bool , int , long , float and complex 基本数字类型: boolintlongfloatcomplex
  • basic string types: str , unicode (python 2.x only) and bytes (python 3.x only) 基本字符串类型: strunicode (仅限python 2.x)和bytes (仅限python 3.x)
  • tuple (but not list ) tuple (但不是list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM