Content deleted Content added
m →Static versus dynamic: grammar/flow |
m change source to syntaxhighlight |
||
Line 20:
For example, in the [[Python (programming language)|Python]] interactive [[REPL]]:
<
>>> number = 99
>>> first_noun = "problems"
Line 28:
I got 99 problems but a hound ain't one.
</syntaxhighlight>
However, relying on dynamic name resolution in code is discouraged by the Python community.<ref>{{cite web|url=http://mail.python.org/pipermail/python-ideas/2009-May/004582.html|title=<nowiki>[</nowiki>Python-Ideas<nowiki>]</nowiki> str.format utility function|date=9 May 2009|accessdate=2011-01-23}}</ref><ref>{{cite web|url=http://diveintopython.org/html_processing/dictionary_based_string_formatting.html|title=8.6. Dictionary-based string formatting|work=diveintopython.org|publisher=Mark Pilgrim|accessdate=2011-01-23}}</ref> The feature also may be removed in a later version of Python.<ref>{{cite web |url=https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces|title=9. Classes - Python documentation |quote=It is important to realize that scopes are determined textually: the global scope of a function defined in a module is that module’s namespace, no matter from where or by what alias the function is called. On the other hand, the actual search for names is done dynamically, at run time — however, the language definition is evolving towards static name resolution, at “compile” time, so don’t rely on dynamic name resolution! (In fact, local variables are already determined statically.)|accessdate=2019-07-24}}</ref>
Line 40:
For example, the parameter "foo" shadows the local variable "foo" in this common pattern:
<
private int foo; // Name "foo" is declared in the outer scope
Line 53:
return foo;
}
</syntaxhighlight>
Name masking can cause [[Function overloading#Complications|complications in function overloading]], due to overloading not happening across scopes in some languages, notably C++, thus requiring all overloaded functions to be redeclared or explicitly imported into a given namespace.
Line 61:
For example, in this code:
<
class Point {
private:
Line 75:
void setY(double newy) { y = newy; }
}
</syntaxhighlight>
within the <tt>Point</tt> constructor, the class variables <tt>x</tt> and <tt>y</tt> are [[Variable shadowing|shadowed]] by local variables of the same name. This might be alpha-renamed to:
<
class Point {
private:
Line 91:
void setY(double newy) { y = newy; }
}
</syntaxhighlight>
In the new version, there is no masking, so it is immediately obvious which uses correspond to which declarations.
|