mirror of
https://github.com/github/codeql.git
synced 2026-05-02 12:15:17 +02:00
Initial commit of Python queries and QL libraries.
This commit is contained in:
committed by
Mark Shannon
parent
90c75cd362
commit
5f58824d1b
36
python/ql/src/Variables/ShadowGlobal.qhelp
Normal file
36
python/ql/src/Variables/ShadowGlobal.qhelp
Normal file
@@ -0,0 +1,36 @@
|
||||
<!DOCTYPE qhelp PUBLIC
|
||||
"-//Semmle//qhelp//EN"
|
||||
"qhelp.dtd">
|
||||
<qhelp>
|
||||
|
||||
<overview>
|
||||
<p>Python statements can access variables in both the local namespace and in the global namespace.
|
||||
When a local and a global variable have the same name, the local variable "shadows" or "hides" the
|
||||
global variable. When the variable is referenced, the variable with local scope is used unless you
|
||||
explicitly use the <code>global</code> statement to reference the global variable. This can lead to
|
||||
confusion as a reader of the code may expect the variable to refer to a global.
|
||||
</p>
|
||||
|
||||
</overview>
|
||||
<recommendation>
|
||||
|
||||
<p>Avoid using the same name for variables in local and global namespaces.</p>
|
||||
|
||||
</recommendation>
|
||||
<example>
|
||||
<p>The following simple example shows how a local variable can "shadow" a global variable. The local
|
||||
variable should be renamed to make the code easier to interpret.</p>
|
||||
<sample src="ShadowGlobal.py" />
|
||||
|
||||
|
||||
</example>
|
||||
<references>
|
||||
|
||||
<li>J. Lusth, <i>The Art and Craft of Programming - Python Edition</i>, Section: Scope. University of Alabama, 2012. (<a href="http://troll.cs.ua.edu/ACP-PY/index_13.html">Published online</a>).</li>
|
||||
<li>New Mexico Tech Computer Center: <a href="http://infohost.nmt.edu/tcc/help/pubs/python/web/global-statement.html">The global
|
||||
statement: Declare access to a global name</a>.</li>
|
||||
|
||||
|
||||
|
||||
</references>
|
||||
</qhelp>
|
||||
Reference in New Issue
Block a user