English 中文(简体)
p是否得到正式支持,在 lo后可重新使用 lo。
原标题:Does python officially support reusing a loop-variable after the loop?
  • 时间:2012-05-12 12:12:54
  •  标签:
  • python

下述法规不正确吗?

for i in some_values:
    do_whatever(i)
do_more_things(i)

不管怎样,它觉得像变式<代码>i”一样,仍然属于排在路段内。 然而,第2.7号,让我在休息后再重复使用。

p是否正式支持这一特点,或我是否滥用该语言?

最佳回答

是的。

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

> The target list is not deleted when the loop is finished 

请注意:target list 。 不仅仅是一个变量:

for some_list[blah] in...
for some_object.foo in...
for a[:n] in ...:

等等。 这些事情不能在 lo后消失。

问题回答

您可以利用这一技术,对符合某些标准的物品清单进行扫描:

for item in iter:
    if interesting(item):
        break
else:
    raise ValueError( iter is boring. )

handle(item)

正如@petr所说的那样,它不自然。 不是因为它允许它自然而然,就是你必须使用它。

我有这样的一些东西,尽管它可能不适用于带<条码>的逻辑。 使用情况:

for i in some_values:
    do_whatever(i)
else:
    do_more_things(i)

但是,如果<条码>有些——价值<>/代码>评价为空洞,但更清楚。 它没有给出内地面积的明确可读性,但养蜂可能表明这一点。

但正如其他人所说的那样,为了回答具体的《任择议定书》问题,是,它是合法的。

这并不是一个特点。 你写道,变量在范围上仍然存在。 它是正常的口译行为。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签