列表中的元组 [英] list in a tuple

查看:129
本文介绍了列表中的元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在programming.reddit.com上讨论了

在下列情况下会发生什么:

Recently, I got into a debate on programming.reddit.com about
what should happen in the following case:


>> a =([1],2)
a [0] + = [3]
>>a = ([1], 2)
a[0] += [3]



目前,Python引发了一个错误*并且*改变元组的第一个元素
元组。现在,这似乎是一个人想要改变的事情 - 为什么要提出错误*和*执行它的事情

抱怨?讨论似乎没有结束,并且

这就是我在这里发帖的原因。我想知道对这个群体的人们有什么意见

...我真的错误地认为这是奇怪的和不受欢迎的行为吗?顺便说一下,我明白了*为什么*这是发生了什么?我认为它应该改变......

这是开始讨论的帖子:
< a rel =nofollowhref =http://filoxus.blogspot.com/2007/12/python-3000-how-mutable-is-immutable.html#links\"target =_ blank> http:// filoxus .blogspot.com / 2007/12 / ... ble.html#links

感谢您的回复

Currently, Python raises an error *and* changes the first element of
the tuple. Now, this seems like something one would want to
change - why raise an error *and* execute the thing it
was complaining about? The discussion seems to have no end, and
that is why I''m posting here. I would like to know what is the opinion
of the people on this group... Am I really mistaking for thinking that
this is strange and unwanted behavior? Btw I understand *why* is this
happening, I just think it should change...
And here is the post that started this discussion:
http://filoxus.blogspot.com/2007/12/...ble.html#links

Thanks for your replies

推荐答案

12月24日,4:13 * pm,montyphy ... @ gmail.com写道:
On Dec 24, 4:13*pm, montyphy...@gmail.com wrote:

就像我说的,它是清楚*为什么*这种情况发生了,我关心的是,如果我们*想要*发生这种情况,即如果当前情况令人满意的话,那就是
。你的mytuple课程

会是一个类似于解决方案的东西,我的问题

就是这个群体中的人们对此的看法。
Like I said, it is clear *why* this happens, what I
am concerned is if this what we *want* to happen, i.e.,
if the current situation is satisfying. Your mytuple class
would be something that resembles a solution, my question
is what the people on this group think about it.



我理解你在原来的帖子中说过,但是你没有b $ b解释你认为原因是什么。我已经提供了一个解释

(这可能不完美)有两个原因:

*我认为这对阅读这个帖子的其他人有用。 />
*它为''mytuple''课提供了动力。


我不确定我对它的看法:)


-

Arnaud

I understand you said that in your original post, but you didn''t
explain what you thought the reason was. I''ve provided an explanation
(which might not be perfect) for two reasons:
* I thought it would be useful to other people reading this thread.
* It gave a motivation for the ''mytuple'' class.

I''m not sure what I think about it yet :)

--
Arnaud


12月24日,8:22 * am,montyphy。 .. @ gmail.com写道:
On Dec 24, 8:22*am, montyphy...@gmail.com wrote:

最近,我进入了关于programming.reddit.com的讨论,关于

会发生什么以下情况:
Recently, I got into a debate on programming.reddit.com about
what should happen in the following case:

> a =([1],2)
a [0] + = [ 3]
>a = ([1], 2)
a[0] += [3]



目前,Python引发错误*并且*改变元组的第一个元素
元组。现在,这似乎是一个人想要改变的事情 - 为什么要提出错误*和*执行它的事情

抱怨?


Currently, Python raises an error *and* changes the first element of
the tuple. Now, this seems like something one would want to
change - why raise an error *and* execute the thing it
was complaining about?



哈欠。多个动作已组合成一行。第一个

成功,第二个失败。


如果你需要提交回滚行为,请在

中明确指定它们/除了。我敢打赌,你会发现你并不是真的需要这种行为。对于这样的事情,没有使用混乱和减慢

语言 - Python不是SQL。

Raymond

Yawn. Multiple actions have been combined into one line. The first
succeeds and the second fails.

If you need commit-rollback behaviors, specify them explicitly in a
try/except. My bet is that you''ll figure-out that you didn''t really
need that behavior to begin with. No use cluttering and slowing the
language for something like this -- Python is not SQL.
Raymond


2007年12月24日星期一18:01:53 -0800,Raymond Hettinger写道:
On Mon, 24 Dec 2007 18:01:53 -0800, Raymond Hettinger wrote:

>目前,Python引发错误*和*更改了元组的第一个元素。现在,这似乎是一个人们想要改变的东西 -
为什么会引发错误*并*执行它抱怨的事情?
>Currently, Python raises an error *and* changes the first element of
the tuple. Now, this seems like something one would want to change -
why raise an error *and* execute the thing it was complaining about?



哈欠。多个动作已组合成一行。


Yawn. Multiple actions have been combined into one line.



这是一个好主意吗?

And this is a good idea?


第一个成功,第二个失败。
The first succeeds and the second fails.



这是一个好主意吗?

在调用

__iadd__关于项目,而不是之后?

And this is a good idea?

Shouldn''t the tuple assignment raise the exception BEFORE calling
__iadd__ on the item, instead of after?


如果您需要提交回滚行为,请在
$ b $中明确指定它们b尝试/除外。我敢打赌,你会发现你并不是真的需要这种行为。对于像这样的东西,没有使用混乱和减慢

语言 - Python不是SQL。
If you need commit-rollback behaviors, specify them explicitly in a
try/except. My bet is that you''ll figure-out that you didn''t really
need that behavior to begin with. No use cluttering and slowing the
language for something like this -- Python is not SQL.



谁说过有关提交回滚的内容?


但即使Python不是SQL,半成功的操作也是PITA

,只要它们可以发生,因为你最终必须以各种复杂和丑陋的方式围绕它们进行编码。要不然,或者你最终得到

隐藏错误的奇怪角落。


我从来不是增强任务的忠实粉丝。我认为这与Python粒度相反:它是一个隐含的操作,使用标点符号,对于保存击键或三次击键的唯一(?)好处。


但我认为这种行为算作语言的疣,而不是
a bug。


- -

史蒂文

Who said anything about commit-rollbacks?

But even if Python is not SQL, operations that half-succeed are a PITA
whenever they can occur, because you end up having to code around them in
all sorts of complicated and ugly ways. Either that, or you end up with
odd corner cases hiding bugs.

I was never a big fan of augmented assignments. I think it goes against
the Python grain: it''s an implied operation, using punctuation, for the
sole (?) benefit of saving a keystroke or three.

But I think this behaviour counts as a wart on the language, rather than
a bug.

--
Steven


这篇关于列表中的元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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