page589.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 44 行

HTML
44
字号
<HTML><HEAD><TITLE>Scopes and Namespaces</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html7918" HREF="page590.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7916" HREF="page587.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7912" HREF="page588.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7920" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0017210000000000000000">Scopes and Namespaces</A></H2><P>The <em>scope</em><A NAME=56875>&#160;</A> of a name is the rangeof statements in the text of a program in which that namecan be used to refer to an object.Python defines three scopes--<em>local</em><A NAME=56877>&#160;</A><A NAME=56878>&#160;</A>,<em>global</em><A NAME=56880>&#160;</A><A NAME=56881>&#160;</A>, and<em>built-in</em><A NAME=56883>&#160;</A><A NAME=56884>&#160;</A>.In Python scopes are called <em>namespaces</em><A NAME=56886>&#160;</A>.When a name is used to refer to an object in a Python programthe namespaces are searched in the following orderto find the binding for that name:local namespace first,then global namespace,then built-in namespace.This is the so-called <em>LGB rule</em><A NAME=56888>&#160;</A>.<P>When a Python function is called,a new <em>local</em> namespace is created.By default,name bindings created inside a functionare created in the local namespace of that function.Name bindings created at the top-level of a module (or file)are created in the global namespace.The built-in namespace contains the bindingsfor the pre-defined names of Python.<P><HR><A NAME="tex2html7918" HREF="page590.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7916" HREF="page587.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7912" HREF="page588.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7920" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?