3-17 Function - namespace

Namespaces

Also known name space, by definition is a place to store name, store name? Illustration, if the variable x = 1,1 stored in memory, that name x Where is it stored? It is a place to store the namespace binding relationship with x.

A total of three kinds of name space, are as follows:

  • locals: name space within the function, including local variables and parameter
  • globals: global variables, function module was defined namespace
  • builtins: namespace built-in module

Scope of different variables is different from the namespace where the decision of this variable.

Scope That range:

  • Global scope: global survival, globally effective
  • Local scope: Temporary survival, local effective

View Scope Methods: globals () locals ()

>>> x = 1
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <cl
ass '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {
}, '__builtins__': <module 'builtins' (built-in)>, 'x': 1}

>>> locals() #现在所在的位置命名空间中的内容
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <cl
ass '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {
}, '__builtins__': <module 'builtins' (built-in)>, 'x': 1}
>>> __builtins__
<module 'builtins' (built-in)>
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'Blocki
ngIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError
', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'Conne
ctionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentErro
r', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPoint
Error', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarni

 

Guess you like

Origin www.cnblogs.com/echo-kid-coding/p/11289250.html