为什么整数在Python中是不可变的? [英] Why are integers immutable in Python?

查看:236
本文介绍了为什么整数在Python中是不可变的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解Python中可变对象和不可变对象之间的区别.我已经阅读了许多讨论差异的文章.但是,我还没有读到任何有关WHY整数是不可变对象的信息.

I understand the differences between mutable and immutable objects in Python. I have read many posts discussing the differences. However, I have not read anything regarding WHY integers are immutable objects.

是否存在这个原因?还是答案是就是这样"?

Does there exist a reason for this? Or is the answer "that's just how it is"?

由于似乎是先前提出的问题,系统提示我将这个问题与其他问题区分开".但是,我相信我要问的更多是哲学性的Python问题,而不是技术性的Python问题.

I am getting prompted to 'differentiate' this question from other questions as it seems to be a previously asked question. However, I believe what I'm asking is more of a philosophical Python question rather than a technical Python question.

似乎Python中的原始"对象(即字符串,布尔值,数字等)是不可变的.我还注意到,由基元(即字典,列表,类)组成的派生数据类型是可变的.

It appears that 'primitive' objects in Python (i.e. strings, booleans, numbers, etc.) are immutable. I've also noticed that derived data types that are made up of primitives (i.e. dicts, lists, classes) are mutable.

画线是否是可变的?原始还是衍生?

Is the where the line is drawn whether or not an object is mutable? Primitive vs derived?

推荐答案

使整数可变是与我们惯常使用它们的方式非常不合常理的.

Making integers mutable would be very counter-intuitive to the way we are used to working with them.

考虑以下代码片段:

a = 1       # assign 1 to a
b = a+2     # assign 3 to b, leave a at 1

执行完这些赋值后,我们希望 a 的值为1,而 b 的值为3.加法运算是根据整数创建一个新的整数值存储在 a 和整数2的实例中. 如果加法运算只是将 a 处的整数取值并对其进行了突变,则 a b 的值都将为3.

After these assignments are executed we expect a to have the value 1 and b to have the value 3. The addition operation is creating a new integer value from the integer stored in a and an instance of the integer 2. If the addition operation just took the integer at a and just mutated it then both a and b would have the value 3.

因此,我们希望算术运算会为其结果创建新值-而不会使它们的输入参数发生变化.

So we expect arithmetic operations to create new values for their results - not to mutate their input parameters.

但是,在某些情况下,对数据结构进行更改更方便,更有效.让我们暂时假设list.append(x)没有修改list,但是返回了附加了xlist的新副本. 然后是一个像这样的函数:

However, there are cases where mutating a data structure is more convenient and more efficient. Let's suppose for the moment that list.append(x) did not modify list but returned a new copy of list with x appended. Then a function like this:

def foo():
  nums = []
  for x in range(0,10):
    nums.append(x)
  return nums

将只返回空列表. (请记住-在这里nums.append(x)不会更改nums-它会返回添加了x的新列表.但是此新列表不会保存在任何地方.)

would just return the empty list. (Remember - here nums.append(x) doesn't alter nums - it returns a new list with x appended. But this new list isn't saved anywhere.)

我们必须像这样编写foo例程:

We would have to write the foo routine like this:

def foo():
  nums = []
  for x in range(0,10):
    nums = nums.append(x)
  return nums

(实际上,这与Python字符串的情况非常相似,直到大约2.6或2.5.)

(This, in fact, is very similar to the situation with Python strings up until about 2.6 or perhaps 2.5.)

此外,每次我们分配nums = nums.append(x)时,我们都会复制一个列表,该列表的大小会增加,从而导致二次行为. 由于这些原因,我们使列表成为 mutable 对象.

Moreover, every time we assign nums = nums.append(x) we would be copying a list that is increasing in size resulting in quadratic behavior. For those reasons we make lists mutable objects.

使列表可变的结果是在这些语句之后:

A consequence to making lists mutable is that after these statements:

a = [1,2,3]
b = a
a.append(4)

列表 b 已更改为[1,2,3,4].这是我们共同生活的东西,尽管它仍然时不时地绊倒我们.

the list b has changed to [1,2,3,4]. This is something that we live with even though it still trips us up now and then.

这篇关于为什么整数在Python中是不可变的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆