为什么我们需要 Python 中的元组(或任何不可变数据类型)? [英] Why do we need tuples in Python (or any immutable data type)?

查看:12
本文介绍了为什么我们需要 Python 中的元组(或任何不可变数据类型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些 Python 教程(其中之一是 Dive Into Python),以及 Python.org 上的语言参考 - 我不明白为什么该语言需要元组.

I've read several python tutorials (Dive Into Python, for one), and the language reference on Python.org - I don't see why the language needs tuples.

与列表或集合相比,元组没有方法,如果我必须将元组转换为集合或列表才能对它们进行排序,那么首先使用元组有什么意义?

Tuples have no methods compared to a list or set, and if I must convert a tuple to a set or list to be able to sort them, what's the point of using a tuple in the first place?

不变性?

为什么有人会关心变量在内存中的位置是否与最初分配时不同?Python 中的整个不可变性业务似乎被过分强调了.

Why does anyone care if a variable lives at a different place in memory than when it was originally allocated? This whole business of immutability in Python seems to be over emphasized.

在 C/C++ 中,如果我分配一个指针并指向某个有效内存,我不关心地址的位置,只要在我使用它之前它不为空即可.

In C/C++ if I allocate a pointer and point to some valid memory, I don't care where the address is located as long as it's not null before I use it.

每当我引用该变量时,我不需要知道指针是否仍指向原始地址.我只是检查 null 并使用它(或不使用它).

Whenever I reference that variable, I don't need to know if the pointer is still pointing to the original address or not. I just check for null and use it (or not).

在 Python 中,当我分配一个字符串(或元组)将其分配给 x,然后修改该字符串时,我为什么要关心它是否是原始对象?只要变量指向我的数据,这就是一切.

In Python, when I allocate a string (or tuple) assign it to x, then modify the string, why do I care if it's the original object? As long as the variable points to my data, that's all that matters.

>>> x='hello'
>>> id(x)
1234567
>>> x='good bye'
>>> id(x)
5432167

x 还是引用了我要的数据,为什么还要关心它的id是相同还是不同?

x still references the data I want, why does anyone need to care if its id is the same or different?

推荐答案

  1. 不可变对象可以进行大量优化;这大概就是为什么字符串在 Java 中也是不可变的,Java 是完全独立开发但与 Python 几乎同时开发的,而且在真正的函数式语言中几乎所有东西都是不可变的.

  1. immutable objects can allow substantial optimization; this is presumably why strings are also immutable in Java, developed quite separately but about the same time as Python, and just about everything is immutable in truly-functional languages.

特别是在 Python 中,只有不可变对象可以是可散列的(因此,集合的成员或字典中的键).同样,这提供了优化,但不仅仅是实质性"(设计体面的哈希表存储完全可变的对象是一场噩梦——要么你在散列后立即复制所有内容,要么检查对象的散列是否是噩梦自从你上次提到它后,它已经变了.

in Python in particular, only immutables can be hashable (and, therefore, members of sets, or keys in dictionaries). Again, this afford optimization, but far more than just "substantial" (designing decent hash tables storing completely mutable objects is a nightmare -- either you take copies of everything as soon as you hash it, or the nightmare of checking whether the object's hash has changed since you last took a reference to it rears its ugly head).

优化问题示例:

$ python -mtimeit '["fee", "fie", "fo", "fum"]'
1000000 loops, best of 3: 0.432 usec per loop
$ python -mtimeit '("fee", "fie", "fo", "fum")'
10000000 loops, best of 3: 0.0563 usec per loop

这篇关于为什么我们需要 Python 中的元组(或任何不可变数据类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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