Variable declaration reset: Difference between revisions

Content added Content deleted
(C++ update)
Line 466: Line 466:


=={{header|Python}}==
=={{header|Python}}==
In Python, variables are not declared before use. If you assign to a non-existent variable, you create a new variable with that name. The language does not prohibit writing code to read a variable that may or may not exist. But if the code tries at runtime to read a variable that happens to not have been assigned to (or a variable that happens to have been `del`'ed), you get a `NameError` exception at runtime.
In Python, variables are not declared before use. If you assign to a non-existent variable, you create a new variable with that name. The language does not prohibit writing code to read a variable that may or may not exist. But if the code tries at runtime to read a variable that happens to not have been assigned to (or a variable that happens to have been <code>del</code>'ed), you get a <code>NameError</code> exception at runtime.


The following code is legal, but note that a Python code checker such as pyflakes will flag such code with an error.
The following code is legal, but note that a Python code checker such as pyflakes will flag such code with an error.