当我将新元素添加到TUPLE时为何起作用? [英] Why does it work when I append a new element to a TUPLE?

查看:52
本文介绍了当我将新元素添加到TUPLE时为何起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于元组在Python中是不可更改的数据类型,而元组理解也不是问题,那么为什么用圆括号而不是方括号的List表达式能很好地工作并产生规则的元组呢?

Since Tuples are non-mutable data types in Python and Tuple comprehensions aren't a thing then why do List comprehensions with circle brackets instead of square brackets work fine and produce regular Tuples?

我认为圆括号用于定义元组而不是列表(我知道那里没有错).

I thought circular brackets were used to define Tuples not Lists (I know I'm not wrong there).

根据我的理解,我将值添加到已经定义的元组中,并且能够更新元组,并且由于元组是不可变的,所以这不应该发生(在Python中).

From my understanding I'm appending values to a Tuple that has already been defined and I'm able to update a Tuple and that's not supposed to happen (in Python) as Tuples are non-mutable.

我没有收到任何错误,请告诉我对此有一个合理的解释.

I'm not getting any errors, someone please tell me there is a rational explanation to this.

这是我的代码:

ignore = (r"^\@define\s")
x = tuple()
(x.append(True if re.search(regex, line) else False) for regex in ignore)
if True in x:
  print("How is this possible?")

推荐答案

由于使用的是生成器表达式,因此不会引发错误.

An error isn't being thrown because you are using a generator expression.

>>> x = tuple()
>>> x
()
>>> (x.append(i) for i in range(10))
<generator object <genexpr> at 0x110bede60>

对生成器的计算是惰性的,并且由于您甚至没有将生成器捕获到变量中,因此只会收集垃圾.但是,看看当我确实使用生成器时会发生什么:

Generators are evaluated lazily, and since you haven't even captured the generator in a variable, it just gets garbage collected. However, look what happens when I do use the generator:

>>> g = (x.append(i) for i in range(10))
>>> next(g)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <genexpr>
AttributeError: 'tuple' object has no attribute 'append'

generator 是编写迭代器的一种快速、酷炫的方式. generator 表达式本质上是生成器的列表理解",但是您可以使用 yield :

A generator is a quick, cool way to write iterators. A generator expression is essentially a "list comprehension for generators", but you can write a generator by using yield:

>>> def my_generator():
...   yield 1
...   yield 3
...
>>> g = my_generator()
>>> next(g)
1
>>> next(g)
3
>>> next(g)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> list(my_generator())
[1, 3]
>>>

现在,关于生成器表达式的很酷的事情是,您可以将它们与 tuple 构造函数结合使用,该构造函数需要进行任何迭代,并且您对穷人的tuple的理解有些复杂!*请注意,如果只有一个参数,可以将括号放在函数参数中:

Now, the cool thing about generator expressions is you can combine them with the tuple constructor, which takes any iterable, and you sort of have a poor-man's tuple comprehension! *Note, you can drop the parentheses in a function argument if there is only one parameter :

>>> tuple(x for x in range(20))
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
>>>

顺便说一句,您应该从不运行这样的代码:

As an aside, you should never run code like this:

>>> x = []
>>> [x.append(i) for i in range(10)]
[None, None, None, None, None, None, None, None, None, None]
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

首先,这很浪费,因为它创建了一个完全没有用的 None 列表.其次,它将功能构造,列表理解与状态更改( .append )混合在一起,这是一种不好的形式.Python程序员期望列表理解不能做到这一点.只需为命令代码使用for循环.

Firstly, it's wasteful because it creates a list of None that is totally useless. Second, it is mixing a functional construct, a list comprehension, with state change (.append), which is bad form. Python programmers expect list comprehensions not to do that. Just use a for-loop for imperative code.

这篇关于当我将新元素添加到TUPLE时为何起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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