循环范围 [英] loop scope

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

问题描述

a = [1,2,3]
for p in a:
print p $ /
1

2

3 p
3


我天真的期望是那个p将来自外部''未定义'

循环。


我知道这不是新闻。


事实上我曾经碰到过一些关于这个事实的讨论,但显然

没有注册它。


因为我让自己感到惊讶通过它 - 特别是在列表

comps的背景下,我认为这是特别令人惊讶的:

b = [4,5,6]
[t * 2 for t in b]
[8,10,12] t
a=[1,2,3]
for p in a: print p
1
2
3 p 3

My naive expectation was that p would be ''not defined'' from outside
the loop.

I know this is not news.

In fact I had come across some discussion of this fact, but apparently
didn''t register it.

As I got myself surprised by it - specifically in the context of list
comps, where I think it is particularly surprising:
b=[4,5,6]
[t*2 for t in b] [8, 10, 12] t






这是否有用,或更多的是神器?


艺术


6

Is this anywhere useful, or more of an artifact?

Art

推荐答案



" Arthur" < AJ ****** @ optonline.com>在留言中写道

news:f8 ******************************** @ 4ax.com ...

"Arthur" <aj******@optonline.com> wrote in message
news:f8********************************@4ax.com...
a = [1,2,3]
for p in a:print p 1
2
3 p 3

我天真的期望是p将从外部未定义循环。
<我知道这不是新闻。

事实上我曾经对这个事实进行了一些讨论,但显然没有注册它。
当我对此感到惊讶时 - 特别是在列表
comps的背景下,我认为这是特别令人惊讶的:
b = [4,5,6]
[t * 2 for t in b] [8,10,12] t
a=[1,2,3]
for p in a: print p
1
2
3 p 3

My naive expectation was that p would be ''not defined'' from outside
the loop.

I know this is not news.

In fact I had come across some discussion of this fact, but apparently
didn''t register it.

As I got myself surprised by it - specifically in the context of list
comps, where I think it is particularly surprising:
b=[4,5,6]
[t*2 for t in b] [8, 10, 12] t


6

这在任何地方都有用,还是更多的神器?


6

Is this anywhere useful, or more of an artifact?




对于带有break语句的循环,当

中断发生时循环索引的值可能是计算它所需要的答案。对于列表

comps哪些不能休息,这是一个可能会消失的神器

2.4。


Terry J. Reedy




For loops with a break statement, the value of the loop index when the
break occurs may be the answer sought or needed to calculate it. For list
comps which cannot have break, it is an artifact which may disappear in
2.4.

Terry J. Reedy



2004年3月11日星期四21:08:09 GMT,Arthur< aj ****** @ optonline.com> ;

写道:
On Thu, 11 Mar 2004 21:08:09 GMT, Arthur <aj******@optonline.com>
wrote:
a = [1,2,3]
for p in a:print p
1
3 p3

我天真的期望是p将从外部''未定义''循环。

我知道这不是新闻。

事实上我曾经碰到过一些关于这个事实的讨论,但显然没有'注册它。

因为我对此感到惊讶 - 特别是在列表
comps的上下文中,我认为它特别令人惊讶:
b = [4,5 ,6]
[t * 2 for t in b] [8,10,12] t
a=[1,2,3]
for p in a: print p
1
2
3 p3

My naive expectation was that p would be ''not defined'' from outside
the loop.

I know this is not news.

In fact I had come across some discussion of this fact, but apparently
didn''t register it.

As I got myself surprised by it - specifically in the context of list
comps, where I think it is particularly surprising:
b=[4,5,6]
[t*2 for t in b][8, 10, 12] t


6

这在任何地方都有用,或更多的神器?


6

Is this anywhere useful, or more of an artifact?




范围由函数,类和

模块的边界定义,而不是循环。这是必不可少的,或者你必须在大多数for循环中声明全局变量。


对于范围的一个好的,最新的讨论,参见学习Python,Ch。

13范围和参数,以及p.337 - 命名空间:整个故事。是

肯定会获得第2版。自第一版以来,范围规则已大幅改变




- 戴夫



Scopes are defined by the boundaries of functions, classes and
modules, not for loops. This is essential, or you would have to
declare global variables inside most for loops.

For a good, up-to-date discussion of scopes, see Learning Python, Ch.
13 Scopes and Arguments, and p.337 - Namespaces: The Whole Story. Be
sure to get the 2nd edition. The scope rules have changed
significantly since the first edition.

-- Dave


Terry Reedy写道:
Terry Reedy wrote:
对于带有break语句的循环,
中断发生时循环索引的值可能是计算它所需的答案或需要的答案。对于无法破解的列表
comps,它可能会消失在
2.4中。
For loops with a break statement, the value of the loop index when the
break occurs may be the answer sought or needed to calculate it. For list
comps which cannot have break, it is an artifact which may disappear in
2.4.




如果我理解你,我不会我想我喜欢这个主意。它
增加了循环心理模型的复杂性。

循环变量在循环结束时消失,除非

循环包含中断。


在我看来, Python应该选择一个模型并坚持

它。循环变量是普通变量,即

存在,直到你明确删除它们,或者它们是

特殊变量自动消失在

结尾循环。我投票给第一个。

-

史蒂芬德阿普拉诺



If I understand you, I don''t think I like this idea. It
adds complexity to the mental model of a loop. "The
loop variable disappears when the loop ends unless the
loop contains a break".

In my opinion, Python should pick a model and stick to
it. Loop variables are either ordinary variables that
exist until you explicitly delete them, or they are
special variables that automagically disappear at the
end of the loop. I vote for the first one.
--
Steven D''Aprano


这篇关于循环范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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