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

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

      <tfoot id='PYmZ7'></tfoot>
        • <bdo id='PYmZ7'></bdo><ul id='PYmZ7'></ul>

        将代码添加到 __init__.py

        时间:2023-09-14
        • <bdo id='hVlCk'></bdo><ul id='hVlCk'></ul>

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

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

          2. <tfoot id='hVlCk'></tfoot>
            1. <legend id='hVlCk'><style id='hVlCk'><dir id='hVlCk'><q id='hVlCk'></q></dir></style></legend>
                  本文介绍了将代码添加到 __init__.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在查看 django 中的模型系统是如何工作的,我发现了一些我不明白的地方.

                  I'm taking a look at how the model system in django works and I noticed something that I don't understand.

                  我知道你创建了一个空的 __init__.py 文件来指定当前目录是一个包.并且您可以在 __init__.py 中设置一些变量,以便 import * 正常工作.

                  I know that you create an empty __init__.py file to specify that the current directory is a package. And that you can set some variable in __init__.py so that import * works properly.

                  但是django在__init__.py中添加了一堆from ... import ...语句并定义了一堆类.为什么?这不是让事情看起来很乱吗?__init__.py 中是否有需要此代码的原因?

                  But django adds a bunch of from ... import ... statements and defines a bunch of classes in __init__.py. Why? Doesn't this just make things look messy? Is there a reason that requires this code in __init__.py?

                  推荐答案

                  当你导入包含它的包(目录)时,__init__.py 中的所有导入都可用.

                  All imports in __init__.py are made available when you import the package (directory) that contains it.

                  例子:

                  ./dir/__init__.py:

                  import something
                  

                  ./test.py:

                  import dir
                  # can now use dir.something
                  

                  忘了提一下,__init__.py 中的代码在您第一次从该目录导入任何模块时运行.所以它通常是放置任何包级初始化代码的好地方.

                  forgot to mention, the code in __init__.py runs the first time you import any module from that directory. So it's normally a good place to put any package-level initialisation code.

                  dgrant 在我的示例中指出了可能的混淆.在 __init__.py import something 可以导入任何模块,不需要从包中导入.例如,我们可以将其替换为 import datetime,然后在我们的顶级 test.py 中,这两个片段都可以工作:

                  dgrant pointed out to a possible confusion in my example. In __init__.py import something can import any module, not necessary from the package. For example, we can replace it with import datetime, then in our top level test.py both of these snippets will work:

                  import dir
                  print dir.datetime.datetime.now()
                  

                  import dir.some_module_in_dir
                  print dir.datetime.datetime.now()
                  

                  底线是:在 __init__.py 中分配的所有名称,无论是导入的模块、函数还是类,在您导入包或包中的模块时自动在包命名空间中可用.

                  The bottom line is: all names assigned in __init__.py, be it imported modules, functions or classes, are automatically available in the package namespace whenever you import the package or a module in the package.

                  这篇关于将代码添加到 __init__.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:点安装.仅创建 dist-info 而不是包 下一篇:pip 无法从 requirements.txt 安装软件包

                  相关文章

                  最新文章

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

                  <legend id='7VYeY'><style id='7VYeY'><dir id='7VYeY'><q id='7VYeY'></q></dir></style></legend>

                      <tfoot id='7VYeY'></tfoot>
                    1. <small id='7VYeY'></small><noframes id='7VYeY'>