<tfoot id='Lel7W'></tfoot>

    1. <small id='Lel7W'></small><noframes id='Lel7W'>

      <legend id='Lel7W'><style id='Lel7W'><dir id='Lel7W'><q id='Lel7W'></q></dir></style></legend>
          <bdo id='Lel7W'></bdo><ul id='Lel7W'></ul>
      1. <i id='Lel7W'><tr id='Lel7W'><dt id='Lel7W'><q id='Lel7W'><span id='Lel7W'><b id='Lel7W'><form id='Lel7W'><ins id='Lel7W'></ins><ul id='Lel7W'></ul><sub id='Lel7W'></sub></form><legend id='Lel7W'></legend><bdo id='Lel7W'><pre id='Lel7W'><center id='Lel7W'></center></pre></bdo></b><th id='Lel7W'></th></span></q></dt></tr></i><div id='Lel7W'><tfoot id='Lel7W'></tfoot><dl id='Lel7W'><fieldset id='Lel7W'></fieldset></dl></div>

        “import *"到底是什么?进口?

        时间:2023-09-10

          <small id='eyv85'></small><noframes id='eyv85'>

              <bdo id='eyv85'></bdo><ul id='eyv85'></ul>
              <tfoot id='eyv85'></tfoot><legend id='eyv85'><style id='eyv85'><dir id='eyv85'><q id='eyv85'></q></dir></style></legend>

              <i id='eyv85'><tr id='eyv85'><dt id='eyv85'><q id='eyv85'><span id='eyv85'><b id='eyv85'><form id='eyv85'><ins id='eyv85'></ins><ul id='eyv85'></ul><sub id='eyv85'></sub></form><legend id='eyv85'></legend><bdo id='eyv85'><pre id='eyv85'><center id='eyv85'></center></pre></bdo></b><th id='eyv85'></th></span></q></dt></tr></i><div id='eyv85'><tfoot id='eyv85'></tfoot><dl id='eyv85'><fieldset id='eyv85'></fieldset></dl></div>
                <tbody id='eyv85'></tbody>
                • 本文介绍了“import *"到底是什么?进口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  In Python, what exactly does import * import? Does it import __init__.py found in the containing folder?

                  For example, is it necessary to declare from project.model import __init__, or is from project.model import * sufficient?

                  解决方案

                  The "advantage" of from xyz import * as opposed to other forms of import is that it imports everything (well, almost... [see (a) below] everything) from the designated module under the current module. This allows using the various objects (variables, classes, methods...) from the imported module without prefixing them with the module's name. For example

                  >>> from math import *
                  >>>pi
                  3.141592653589793
                  >>>sin(pi/2)
                  >>>1.0
                  

                  This practice (of importing * into the current namespace) is however discouraged because it

                  • provides the opportunity for namespace collisions (say if you had a variable name pi prior to the import)
                  • may be inefficient, if the number of objects imported is big
                  • doesn't explicitly document the origin of the variable/method/class (it is nice to have this "self documentation" of the program for future visit into the code)

                  Typically we therefore limit this import * practice to ad-hoc tests and the like. As pointed out by @Denilson-Sá-Maia, some libraries such as (e.g. pygame) have a sub-module where all the most commonly used constants and functions are defined and such sub-modules are effectively designed to be imported with import *. Other than with these special sub-modules, it is otherwise preferable to ...:

                  explicitly import a few objects only

                  >>>from math import pi
                  >>>pi
                  >>>3.141592653589793
                  >>> sin(pi/2)
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  NameError: name 'sin' is not defined
                  

                  or import the module under its own namespace (or an alias thereof, in particular if this is a long name, and the program references its objects many times)

                    >>>import math
                    >>>math.pi
                    >>>3.141592653589793
                    etc..
                  
                  
                    >>>import math as m  #bad example math being so short and standard...
                    >>>m.pi
                    >>>3.141592653589793
                    etc..
                  

                  See the Python documentation on this topic

                  (a) Specifically, what gets imported with from xyz import * ?
                  if xyz module defines an __all__ variable, it will import all the names defined in this sequence, otherwise it will import all names, except these which start with an underscore.

                  Note Many libraries have sub-modules. For example the standard library urllib includes sub-modules like urllib.request, urllib.errors, urllib.response etc. A common point of confusion is that

                  from urllib import *

                  would import all these sub-modules. That is NOT the case: one needs to explicitly imports these separately with, say, from urllib.request import * etc. This incidentally is not specific to import *, plain import will not import sub-modules either (but of course, the * which is often a shorthand for "everything" may mislead people in thinking that all sub-modules and everything else would be imported).

                  这篇关于“import *"到底是什么?进口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从 python 数据框插入 DB2 表 下一篇:Python ElementTree 模块:使用“find"、“findall&qu

                  相关文章

                  最新文章

                  <tfoot id='Qa6wT'></tfoot>
                  • <bdo id='Qa6wT'></bdo><ul id='Qa6wT'></ul>
                • <i id='Qa6wT'><tr id='Qa6wT'><dt id='Qa6wT'><q id='Qa6wT'><span id='Qa6wT'><b id='Qa6wT'><form id='Qa6wT'><ins id='Qa6wT'></ins><ul id='Qa6wT'></ul><sub id='Qa6wT'></sub></form><legend id='Qa6wT'></legend><bdo id='Qa6wT'><pre id='Qa6wT'><center id='Qa6wT'></center></pre></bdo></b><th id='Qa6wT'></th></span></q></dt></tr></i><div id='Qa6wT'><tfoot id='Qa6wT'></tfoot><dl id='Qa6wT'><fieldset id='Qa6wT'></fieldset></dl></div>

                    <legend id='Qa6wT'><style id='Qa6wT'><dir id='Qa6wT'><q id='Qa6wT'></q></dir></style></legend>
                    1. <small id='Qa6wT'></small><noframes id='Qa6wT'>