Name resolution (programming languages): Difference between revisions

Content deleted Content added
Aasasd (talk | contribs)
No edit summary
m Static versus dynamic: lang="pycon"
Line 21:
 
For example, in [[Python (programming language)|Python]]:
<source lang="pythonpycon">
>>> locals()['num'] = 999 # equivalent to: num = 999
>>> noun = "troubles"
>>> noun2 = "hound"
>>> # which variables to use are decided at runtime
>>> print("{num} {noun} and a {noun2} ain't one".format(**locals()))
# outputs: 999 troubles and a hound ain't one
 
# which variables to use are decided at runtime
print("{num} {noun} and a {noun2} ain't one".format(**locals()))
 
# outputs: 999 troubles and a hound ain't one
</source>
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/tutorial/classes.html#python-scopes-and-namespaces|title=9. Classes - Python v2.7.1 documentation |quote=search for names is done dynamically, at run time — however, the language definition is evolving towards static name resolution|accessdate=2011-01-23}}</ref>