Note the scope of variables in the for loop.

Source: Internet
Author: User
Note the scope of variables in the for loop.
for e in collections:    pass

In the for loop, the last object e always exists in the context. That is, the reference to e is still valid outside the loop.

This problem is easy to ignore. If an object with the same name already exists before the loop, the object will be overwritten.

In code-aware IDE, the IDE will prompt that the variable is "re-declared", but there will be no error at runtime.

The for loop is not a closure. you can use the dis module to break down the following code:

x = 5for x in range(10):    passprint x

Save the code to the test. py file and run python-m dis test. py.


C: \ Users \ Patrick \ Desktop> python-m dis test. py

1 0 LOAD_CONST 0 (5)

3 STORE_NAME 0 (x)


3 6 SETUP_LOOP 20 (to 29)

9 LOAD_NAME 1 (range)

12 LOAD_CONST 1 (10)

15 CALL_FUNCTION 1

18 GET_ITER

> 19 FOR_ITER 6 (to 28)

22 STORE_NAME 0 (x)


4 25 JUMP_ABSOLUTE 19

> 28 POP_BLOCK


6> 29 LOAD_NAME 0 (x)

32 PRINT_ITEM

33 PRINT_NEWLINE

34 LOAD_CONST 2 (None)

37 RETURN_VALUE

In other languages, the initialization variables of the for loop are also visible to the context, for example, java. Because java is a strongly-typed language, if you re-declare an existing variable, the IDE will prompt an error, of course it is different through compilation.

Usually in python Programming (probably the majority of dynamic languages), sometimes even if a variable with the same name is declared, there is no obvious error in the program, but once an error occurs, the error is hard to be found. Avoid duplicate names with variables in the for loop.

This is especially true when using python template language encoding. No prompt is displayed in the code editor and no error is found. This is an extremely weird example. Why is it weird, because there is no logic problem.

In a page template, when handler calls this template, two objects (from handler, I use tornado), one page object and one pages list are passed at the same time. My order is as follows:

{Page. name if page else ''}}

Parent Page

{% If pages %}{% For page in pages %} {Page. name }}{% End %}{% End %} None


{Page. markdown if page else ''}}

The problem arises. An error occurred while running and the prompt is displayed in{Page. name if page else ''}}Error page referenced before assignment.

Dizzy, found the error for one night, and finally changed the name of page in the for loop to _ page to run.

During the template calling process, the template language is also translated to the python bytecode and parsed and exported by line. Therefore, there is no logic at all and it is a bug in the tornado template language.

So pay attention to the variable name.

In short, I think the exception trace of tornado is very unfriendly.

Search sequence of variables in Python: Local scope → Local scope of the current scope embedded (Enclosing locals) → Global) → built-in scope (Built-in)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.