<bdo id='G59dZ'></bdo><ul id='G59dZ'></ul>
    1. <tfoot id='G59dZ'></tfoot>
    2. <i id='G59dZ'><tr id='G59dZ'><dt id='G59dZ'><q id='G59dZ'><span id='G59dZ'><b id='G59dZ'><form id='G59dZ'><ins id='G59dZ'></ins><ul id='G59dZ'></ul><sub id='G59dZ'></sub></form><legend id='G59dZ'></legend><bdo id='G59dZ'><pre id='G59dZ'><center id='G59dZ'></center></pre></bdo></b><th id='G59dZ'></th></span></q></dt></tr></i><div id='G59dZ'><tfoot id='G59dZ'></tfoot><dl id='G59dZ'><fieldset id='G59dZ'></fieldset></dl></div>

      1. <small id='G59dZ'></small><noframes id='G59dZ'>

        <legend id='G59dZ'><style id='G59dZ'><dir id='G59dZ'><q id='G59dZ'></q></dir></style></legend>

        为什么我们需要 Python 中的元组(或任何不可变数据类型)?

        时间:2023-09-01
        <tfoot id='Oq8nu'></tfoot>
              <tbody id='Oq8nu'></tbody>

            <small id='Oq8nu'></small><noframes id='Oq8nu'>

              <bdo id='Oq8nu'></bdo><ul id='Oq8nu'></ul>
              <legend id='Oq8nu'><style id='Oq8nu'><dir id='Oq8nu'><q id='Oq8nu'></q></dir></style></legend>
              <i id='Oq8nu'><tr id='Oq8nu'><dt id='Oq8nu'><q id='Oq8nu'><span id='Oq8nu'><b id='Oq8nu'><form id='Oq8nu'><ins id='Oq8nu'></ins><ul id='Oq8nu'></ul><sub id='Oq8nu'></sub></form><legend id='Oq8nu'></legend><bdo id='Oq8nu'><pre id='Oq8nu'><center id='Oq8nu'></center></pre></bdo></b><th id='Oq8nu'></th></span></q></dt></tr></i><div id='Oq8nu'><tfoot id='Oq8nu'></tfoot><dl id='Oq8nu'><fieldset id='Oq8nu'></fieldset></dl></div>

                1. 本文介绍了为什么我们需要 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 中的元组(或任何不可变数据类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python中方括号和括号括起来的列表有什么区别? 下一篇:Python 中的 *tuple 和 **dict 是什么意思?

                  相关文章

                  1. <small id='XqbA2'></small><noframes id='XqbA2'>

                    <i id='XqbA2'><tr id='XqbA2'><dt id='XqbA2'><q id='XqbA2'><span id='XqbA2'><b id='XqbA2'><form id='XqbA2'><ins id='XqbA2'></ins><ul id='XqbA2'></ul><sub id='XqbA2'></sub></form><legend id='XqbA2'></legend><bdo id='XqbA2'><pre id='XqbA2'><center id='XqbA2'></center></pre></bdo></b><th id='XqbA2'></th></span></q></dt></tr></i><div id='XqbA2'><tfoot id='XqbA2'></tfoot><dl id='XqbA2'><fieldset id='XqbA2'></fieldset></dl></div>

                    • <bdo id='XqbA2'></bdo><ul id='XqbA2'></ul>
                    <tfoot id='XqbA2'></tfoot>
                  2. <legend id='XqbA2'><style id='XqbA2'><dir id='XqbA2'><q id='XqbA2'></q></dir></style></legend>