The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python name binding and name resolution - Python docs 3.11.0
By Guest on 2nd November 2022 08:22:44 AM | Syntax: TEXT | Views: 201



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1.  
  2. 4.2. Naming and binding
  3. =======================
  4.  
  5. 4.2.2. Resolution of names
  6. --------------------------
  7.  
  8. A *scope* defines the visibility of a name within a block.  If a local
  9. variable is defined in a block, its scope includes that block.  If the
  10. definition occurs in a function block, the scope extends to any blocks
  11. contained within the defining one, unless a contained block introduces
  12. a different binding for the name.
  13.  
  14. When a name is used in a code block, it is resolved using the nearest
  15. enclosing scope.  The set of all such scopes visible to a code block
  16. is called the block's *environment*.
  17.  
  18. When a name is not found at all, a "NameError" exception is raised. If
  19. the current scope is a function scope, and the name refers to a local
  20. variable that has not yet been bound to a value at the point where the
  21. name is used, an "UnboundLocalError" exception is raised.
  22. "UnboundLocalError" is a subclass of "NameError".
  23.  
  24. If a name binding operation occurs anywhere within a code block, all
  25. uses of the name within the block are treated as references to the
  26. current block.  This can lead to errors when a name is used within a
  27. block before it is bound.  This rule is subtle.  Python lacks
  28. declarations and allows name binding operations to occur anywhere
  29. within a code block.  The local variables of a code block can be
  30. determined by scanning the entire text of the block for name binding
  31. operations.
  32.  
  33. If the "global" statement occurs within a block, all uses of the names
  34. specified in the statement refer to the bindings of those names in the
  35. top-level namespace.  Names are resolved in the top-level namespace by
  36. searching the global namespace, i.e. the namespace of the module
  37. containing the code block, and the builtins namespace, the namespace
  38. of the module "builtins".  The global namespace is searched first.  If
  39. the names are not found there, the builtins namespace is searched.
  40. The "global" statement must precede all uses of the listed names.
  41.  
  42. The "global" statement has the same scope as a name binding operation
  43. in the same block.  If the nearest enclosing scope for a free variable
  44. contains a global statement, the free variable is treated as a global.
  45.  
  46. The "nonlocal" statement causes corresponding names to refer to
  47. previously bound variables in the nearest enclosing function scope.
  48. "SyntaxError" is raised at compile time if the given name does not
  49. exist in any enclosing function scope.
  50.  
  51. The namespace for a module is automatically created the first time a
  52. module is imported.  The main module for a script is always called
  53. "__main__".
  54.  
  55. Class definition blocks and arguments to "exec()" and "eval()" are
  56. special in the context of name resolution. A class definition is an
  57. executable statement that may use and define names. These references
  58. follow the normal rules for name resolution with an exception that
  59. unbound local variables are looked up in the global namespace. The
  60. namespace of the class definition becomes the attribute dictionary of
  61. the class. The scope of names defined in a class block is limited to
  62. the class block; it does not extend to the code blocks of methods --
  63. this includes comprehensions and generator expressions since they are
  64. implemented using a function scope.  This means that the following
  65. will fail:
  66.  
  67.    class A:
  68.        a = 42
  69.        b = list(a + i for i in range(10))
  70.  
  71.  
  72. Related video
  73. Python Course/Intermediate > Python Scope, Namespaces, Name Resolution and First-Class Objects
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes