Pay attention to 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. Even outside the loop, the subsequent reference to e is still valid.
There is a problem here that is easily overlooked. If an object with the same name already exists before the loop, this object will be overwritten.
If you are in a code-aware IDE, the IDE will prompt that the variable is "redeclared", but there will be no error when running.
The for loop is not a closure, you can use the dis module to decompose the following code as you can see:
x = 5 for x in range(10): pass print x
Save the code to the test.py file and run python -m dis test.py
C:UsersPatrickDesktop> python -m dis test.py 1 3 3 6 SETUP_LOOP 20 (to 29)
9 LOAD_NAME 1 (range)
12 LOAD_CONST 1 10 : 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, such as Java, because Java is a strongly typed language, if you re-declare an existing variable, the IDE will prompt an error, which of course is different from compilation.
Usually in python programming (probably most 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, it is difficult to find the error. So avoid having the same name as the variable in the for loop.
This is especially true when coding in the python template language. The code editor has no prompts and will not find out where the error is. This is an extremely weird example that I encountered. Why do you say weird, because there is no logical problem.
{{ page.name if page else ''}}
The problem is here, an error occurred during operation, the prompt is in {{ page.name if page else ''}}
The error page referenced before assignment.
During the template calling process, the template language is also translated into python bytecode and parsed and output line by line, so there is no logic at all. I don’t know if it is a bug in the tornado template language.
So pay attention to the variable names. In short, I think tornado’s exception trace is very unfriendly. The scope search order of variables in Python: local scope (Local) → local scope where the current scope is embedded (Enclosing locals) → global/module scope (Global) → built-in scope (Built-in)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses the role of virtual environments in Python, focusing on managing project dependencies and avoiding conflicts. It details their creation, activation, and benefits in improving project management and reducing dependency issues.

Fastapi ...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
