简体   繁体   English

在python 3.x中的for循环中,对象是否更改为不可变的?

[英]In a for loop in python 3.x are objects changed to immutable?

In my code pasted bellow (which is python 3 code) I expected the for loop to change the original objects (ie I expected NSTEPx to have been changed by the for loop). 在粘贴下面的代码(是python 3代码)中,我期望for循环可以更改原始对象(即,我希望NSTEPx已被for循环更改)。 Since lists and arrays are mutable I should have edited the object by referring to it by the variable "data". 由于列表和数组是可变的,因此我应该通过变量“ data”引用该对象来编辑该对象。 However, after this code was run, and I called NSTEPx, it was not changed. 但是,运行此代码后,我调用了NSTEPx,但它没有更改。 Can someone explain why this is? 有人可以解释为什么吗? I come from a background of C++ and the idea of mutable and immutable objects is something that I am only recently understanding the nuances of, or so I thought. 我来自C ++的背景,而可变和不可变对象的想法只是我最近才了解的细微差别,我以为如此。

Here is the code: 这是代码:

NSTEPx = np.array(NSTEPx)
TIMEx = np.array(TIMEx)
TEMPx = np.array(TEMPx)
PRESSx = np.array(PRESSx)
Etotx = np.array(Etotx)
EKtotx = np.array(EKtotx)
EPtotx = np.array(EPtotx)
VOLUMEx = np.array(VOLUMEx)
alldata = [NSTEPx,TIMEx,TEMPx, PRESSx, Etotx, EKtotx, EPtotx]
for data in alldata:
    temp = data[1001:-1]
    data = np.insert(data,0,temp)
    data = np.delete(data,np.s_[1001:-1])

In your loop, data refers to an array (some object). 在循环中, data引用数组(某个对象)。 The object referred to is mutable. 引用的对象是可变的。 The variable data can be changed as well to refer to something else, but that won't change what's in alldata (values that refer to objects) or the variables whose contents you implicitly copied to construct alldata . 变量data也可以更改为引用其他内容,但这不会更改alldata (引用对象的值)或隐式复制其内容以构造alldata的变量中的内容。 Hence, all you change is a local variable (implicitly copied from alldata ) to refer to a newly created array. 因此,您alldata更改只是一个局部变量(从alldata隐式复制)以引用新创建的数组。 Any other referring values are unchanged and still refer to the old array. 其他所有引用值均保持不变,但仍引用旧数组。

Python has no assignment! Python 没有分配! data = value is strictly a binding operation, not an assignment. data = value严格来说是绑定操作,而不是赋值。 This is really different then in eg C++ 确实与例如C ++不同

A Python variable is like a label, or a yellow sticky note: you can put it on something or move it to something else; Python变量就像一个标签或一个黄色便签:您可以将其放在其他对象上或将其移动到其他对象上; it does not ( never ) change the thing (object) it is one. 它不会( 永远不会 )改变事物 (对象)为一体。 The =-operator does move the label; =运算符确实会移动标签; it "binds" it. 它“绑定”它。 Although we usually say assign , it is really not the assign of C. (Where it is basically is a memory address) 尽管我们通常说出Assign ,但实际上不是C的赋值。(它基本上是一个内存地址)

To change a value is Python, you need a method: aLabel.do_update() , will (typically) change self , the object itself. 要使用Python更改值,您需要一个方法: aLabel.do_update() ,(通常)将更改self ,即对象本身。 Note aList[....] is a method! 注意aList[....]是一种方法!

So, to change you data: change it (sec). 因此,要更改您的数据:更改它(秒)。 Do not put a other label on it, nor put the existing label on other data! 不要在上面贴上其他标签,也不要在其他数据上贴上现有标签!

Hope this explains you question 希望这能解释您的问题

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

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