久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

    • <bdo id='X8205'></bdo><ul id='X8205'></ul>

      <tfoot id='X8205'></tfoot>

        如何導入在 __init__.py 中定義的類

        How to import classes defined in __init__.py(如何導入在 __init__.py 中定義的類)

        <small id='1nDpH'></small><noframes id='1nDpH'>

        • <bdo id='1nDpH'></bdo><ul id='1nDpH'></ul>
        • <legend id='1nDpH'><style id='1nDpH'><dir id='1nDpH'><q id='1nDpH'></q></dir></style></legend>
        • <tfoot id='1nDpH'></tfoot>

                <tbody id='1nDpH'></tbody>
              • <i id='1nDpH'><tr id='1nDpH'><dt id='1nDpH'><q id='1nDpH'><span id='1nDpH'><b id='1nDpH'><form id='1nDpH'><ins id='1nDpH'></ins><ul id='1nDpH'></ul><sub id='1nDpH'></sub></form><legend id='1nDpH'></legend><bdo id='1nDpH'><pre id='1nDpH'><center id='1nDpH'></center></pre></bdo></b><th id='1nDpH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1nDpH'><tfoot id='1nDpH'></tfoot><dl id='1nDpH'><fieldset id='1nDpH'></fieldset></dl></div>
                  本文介紹了如何導入在 __init__.py 中定義的類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試組織一些模塊供我自己使用.我有這樣的事情:

                  lib/__init__.py設置.py富/__init__.py一些對象.py酒吧/__init__.py別的東西.py

                  lib/__init__.py 中,我想定義一些在導入 lib 時要使用的類.但是,如果不將類分成文件并將它們導入__init__.py,我似乎無法弄清楚.

                  而不是說:

                   庫/__init__.py設置.py助手類.py富/__init__.py一些對象.py酒吧/__init__.py別的東西.py從 lib.settings 導入值從 lib.helperclass 導入助手

                  我想要這樣的東西:

                   庫/__init__.py #Helper 在這個文件中定義設置.py富/__init__.py一些對象.py酒吧/__init__.py別的東西.py從 lib.settings 導入值從庫導入助手

                  有可能嗎,還是我必須將類分離到另一個文件中?

                  編輯

                  好的,如果我從另一個腳本導入 lib,我可以訪問 Helper 類.如何從 settings.py 訪問 Helper 類?

                  此處示例描述了包內引用.我引用子模塊經常需要相互引用".就我而言,lib.settings.py 需要 Helper,而 lib.foo.someobject 需要訪問 Helper,那么我應該在哪里定義 Helper 類?

                  解決方案

                  1. 'lib/ 的父目錄必須在 sys.path 中.

                  2. 您的lib/__init__.py"可能如下所示:

                    來自 .導入設置 # 或者只是在舊 Python 版本上導入設置"類助手(對象):經過

                  那么下面的例子應該可以工作:

                  從 lib.settings 導入值從庫導入助手

                  對問題的編輯版本的回答:

                  __init__.py 定義了你的包從外部看起來如何.如果您需要在 settings.py 中使用 Helper,則在不同的文件中定義 Helper,例如 'lib/helper.py'.

                  <上一頁>.|`-- import_submodule.py`--庫|-- __init__.py|-- 富||-- __init__.py|`--someobject.py|-- 助手.py`-- 設置.py2個目錄,6個文件

                  命令:

                  $ python import_submodule.py

                  輸出:

                  設置幫手lib.settings 中的助手某物lib.foo.someobject 中的助手# ./import_submodule.py導入 fnmatch,操作系統從 lib.settings 導入值從庫導入助手打印對于 os.walk('.') 中的根目錄、目錄和文件:對于 fnmatch.filter(files, '*.py') 中的 f:print "# %s/%s" % (os.path.basename(root), f)打印打開(os.path.join(root, f)).read()打印# lib/helper.py打印幫手"類助手(對象):def __init__(self, module_name):print "Helper in", module_name# lib/settings.py打印設置"導入助手類值(對象):經過helper.Helper(__name__)# lib/__init__.py#from __future__ 導入 absolute_import導入設置、foo.someobject、助手助手 = helper.Helper# foo/someobject.py打印某物"從 .. 導入助手helper.Helper(__name__)# foo/__init__.py導入某個對象

                  I am trying to organize some modules for my own use. I have something like this:

                  lib/
                    __init__.py
                    settings.py
                    foo/
                      __init__.py
                      someobject.py
                    bar/
                      __init__.py
                      somethingelse.py
                  

                  In lib/__init__.py, I want to define some classes to be used if I import lib. However, I can't seem to figure it out without separating the classes into files, and import them in__init__.py.

                  Rather than say:

                      lib/
                        __init__.py
                        settings.py
                        helperclass.py
                        foo/
                          __init__.py
                          someobject.py
                        bar/
                          __init__.py
                          somethingelse.py
                  
                  from lib.settings import Values
                  from lib.helperclass import Helper
                  

                  I want something like this:

                      lib/
                        __init__.py  #Helper defined in this file
                        settings.py
                        foo/
                          __init__.py
                          someobject.py
                        bar/
                          __init__.py
                          somethingelse.py
                  
                  from lib.settings import Values
                  from lib import Helper
                  

                  Is it possible, or do I have to separate the class into another file?

                  EDIT

                  OK, if I import lib from another script, I can access the Helper class. How can I access the Helper class from settings.py?

                  The example here describes Intra-Package References. I quote "submodules often need to refer to each other". In my case, the lib.settings.py needs the Helper and lib.foo.someobject need access to Helper, so where should I define the Helper class?

                  解決方案

                  1. 'lib/'s parent directory must be in sys.path.

                  2. Your 'lib/__init__.py' might look like this:

                    from . import settings # or just 'import settings' on old Python versions
                    class Helper(object):
                          pass
                    

                  Then the following example should work:

                  from lib.settings import Values
                  from lib import Helper
                  

                  Answer to the edited version of the question:

                  __init__.py defines how your package looks from outside. If you need to use Helper in settings.py then define Helper in a different file e.g., 'lib/helper.py'.

                  .
                  |   `-- import_submodule.py
                      `-- lib
                      |-- __init__.py
                      |-- foo
                      |   |-- __init__.py
                      |   `-- someobject.py
                      |-- helper.py
                      `-- settings.py
                  
                  2 directories, 6 files
                  

                  The command:

                  $ python import_submodule.py
                  

                  Output:

                  settings
                  helper
                  Helper in lib.settings
                  someobject
                  Helper in lib.foo.someobject
                  
                  # ./import_submodule.py
                  import fnmatch, os
                  from lib.settings import Values
                  from lib import Helper
                  
                  print
                  for root, dirs, files in os.walk('.'):
                      for f in fnmatch.filter(files, '*.py'):
                          print "# %s/%s" % (os.path.basename(root), f)
                          print open(os.path.join(root, f)).read()
                          print
                  
                  
                  # lib/helper.py
                  print 'helper'
                  class Helper(object):
                      def __init__(self, module_name):
                          print "Helper in", module_name
                  
                  
                  # lib/settings.py
                  print "settings"
                  import helper
                  
                  class Values(object):
                      pass
                  
                  helper.Helper(__name__)
                  
                  
                  # lib/__init__.py
                  #from __future__ import absolute_import
                  import settings, foo.someobject, helper
                  
                  Helper = helper.Helper
                  
                  
                  # foo/someobject.py
                  print "someobject"
                  from .. import helper
                  
                  helper.Helper(__name__)
                  
                  
                  # foo/__init__.py
                  import someobject
                  

                  這篇關于如何導入在 __init__.py 中定義的類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復導入頂級名稱的情況下構造python包)
                  Install python packages on OpenShift(在 OpenShift 上安裝 python 包)
                  How to refresh sys.path?(如何刷新 sys.path?)
                  Distribute a Python package with a compiled dynamic shared library(分發帶有已編譯動態共享庫的 Python 包)
                    <tbody id='CeMHj'></tbody>
                  • <bdo id='CeMHj'></bdo><ul id='CeMHj'></ul>

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

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

                        1. <legend id='CeMHj'><style id='CeMHj'><dir id='CeMHj'><q id='CeMHj'></q></dir></style></legend>
                            <tfoot id='CeMHj'></tfoot>
                            主站蜘蛛池模板: 99视频在线看 | 久久精品视频在线观看 | 毛片在线看片 | 97精品国产97久久久久久免费 | 久久伊 | 欧美激情久久久 | 免费观看成人性生生活片 | 九色一区 | 国产精品毛片无码 | 午夜精品久久久久久久久久久久久 | 国产高清在线精品 | 亚洲福利视频一区二区 | 久久国产精品视频免费看 | 欧美午夜在线 | 九九热免费看 | 日韩精品一区二区三区中文字幕 | 欧美性精品 | 日韩在线视频免费观看 | 中文在线一区二区 | 欧美专区日韩专区 | 欧美日韩视频在线第一区 | 欧美一区成人 | 国产精品自拍视频网站 | 国产精品伦理一区二区三区 | 日本高清不卡视频 | 久久国产精品一区二区三区 | 人人干人人超 | 欧美日韩免费一区二区三区 | 国产精品久久久久久高潮 | 久久不卡日韩美女 | 国产做a爱片久久毛片 | 国产高清久久久 | 精品亚洲一区二区三区四区五区高 | 在线观看成人免费视频 | 少妇一级淫片免费放播放 | 女同av亚洲女人天堂 | 久久99国产精一区二区三区 | 日日碰碰 | 国产欧美日韩一区 | 久久久九九九九 | 成人欧美在线 |