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

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

问题描述

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

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

不变性?

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

在 C/C++ 中,如果我分配一个指针并指向一些有效的内存,我不在乎地址在哪里,只要在我使用它之前它不为空.

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

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

<预><代码>>>>x='你好'>>>编号(x)1234567>>>x='再见'>>>编号(x)5432167

x 仍然引用我想要的数据,为什么有人需要关心它的 id 是相同的还是不同的?

解决方案

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

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

优化问题示例:

$ python -mtimeit '["fee", "fie", "fo", "fum"]'1000000 个循环,最好的 3 个:每个循环 0.432 微秒$ python -mtimeit '("fee", "fie", "fo", "fum")'10000000 个循环,最好的 3 个:每个循环 0.0563 微秒

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?

Immutability?

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.

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.

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).

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 still references the data I want, why does anyone need to care if its id is the same or different?

解决方案

  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.

  2. 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).

Example of optimization issue:

$ 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天全站免登陆