forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShadowGlobal.qhelp
More file actions
36 lines (26 loc) · 1.31 KB
/
ShadowGlobal.qhelp
File metadata and controls
36 lines (26 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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>