<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                要使用模塊和庫,需要先導入。 **Python之禪** ``` >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! ``` 導入系統是相當復雜的,不過你可能已經了解了一些基本知識。這里會介紹一些關于這一子系統的內部機理。 `sys`模塊包含許多關于Python導入系統的信息。首先,當前可導入的模塊列表可以通過`sys.moudle`變量獲取。它是一個字典,其中鍵(key)是模塊名字,對應的值(value)是模塊對象。 ``` >>> sys.modules['os'] <module 'os' from '/usr/lib/python2.7/os.pyc'> ``` 許多模塊是內置的,這些內置的模塊在`sys.builtin_module_names`中列出。內置的模塊可以根據傳入Python構建系統的編譯選項的不同而變化。 導入模塊時,Python會依賴一個路徑列表。這個列表存儲在`sys.path`變量中,并且告訴Python去哪里搜索要加載的模塊。可以在代碼中修改這個列表,根據需要添加或刪除路徑,也可以通過編寫Python代碼直接修改環境變量`PYTHONPATH`。下面的方法幾乎是相等的[①](#anchor21)。 ``` >>> import sys >>> sys.path.append('/foo/bar') $ PYTHONPATH=/foo/bar python >>> import sys >>> '/foo/bar' in sys.path True ``` 在`sys.path`中的順序很重要,因為需要遍歷這個列表來尋找請求的模塊。 也可以通過自定義的導入器(importer)對導入機制進行擴展。Hy[②](#anchor22)正是利用這種技術告訴Python如何導入其他非標準的`.py`或者`.pyc`文件的。 顧名思義,導入鉤子機制是由PEP 302(<http://www.python.org/dev/peps/pep-0302/>)定義的[③](#anchor23)。它允許擴展標準的導入機制,并對其進行預處理,也可以通過追加一個工廠類到`sys.path_hooks`來添加自定義的模塊查找器(finder)。 模塊查找器對象必須有一個返回加載器對象的`find_module(fullname, path=None)`方法,這個加載器對象必須包含一個負責從源文件中加載模塊的`load_module(fullname)`方法。 為了進一步說明,下面給出了Hy利用自定義的導入器導入`.hy`而不是`.py`結尾的源文件的方法,見示例2.1。 **示例2.1 Hy模塊導入器** ``` class MetaImporter(object): def find_on_path(self, fullname): fls = ["%s/__init__.hy", "%s.hy"] dirpath = "/".join(fullname.split(".")) for pth in sys.path: pth = os.path.abspath(pth) for fp in fls: composed_path = fp % ("%s/%s" % (pth, dirpath)) if os.path.exists(composed_path): return composed_path def find_module(self, fullname, path=None): path = self.find_on_path(fullname) if path: return MetaLoader(path) sys.meta_path.append(MetaImporter()) ``` 一旦路徑被確定是有效的且指向了一個模塊,就會返回一個`MetaLoader`對象。 **Hy模塊加載器** ``` class MetaLoader(object): def __init__(self, path): self.path = path def is_package(self, fullname): dirpath = "/".join(fullname.split(".")) for pth in sys.path: pth = os.path.abspath(pth) composed_path = "%s/%s/__init__.hy" % (pth, dirpath) if os.path.exists(composed_path): return True return False def load_module(self, fullname): if fullname in sys.modules: return sys.modules[fullname] if not self.path: return sys.modules[fullname] = None mod = import_file_to_module(fullname, self.path) ? ispkg = self.is_package(fullname) mod.__file__ = self.path mod.__loader__ = self mod.__name__ = fullname if ispkg: mod.__path__ = [] mod.__package__ = fullname else: mod.__package__ = fullname.rpartition('.')[0] sys.modules[fullname] = mod return mod ``` ? `import_file_to_module`讀取一個Hy源文件,將其編譯成Python代碼,并返回一個Python模塊對象。 `uprefix`模塊(<https://pypi.python.org/pypi/uprefix>)是這個功能起作用的另一個好的例子。Python 3.0到3.2并沒有像Python 2中用來表示Unicode字符串的`u`前綴[④](#anchor24),這個模塊通過在編譯前刪除字符串的前綴`u`來確保在2.x和3.x之間的兼容性。 - - - - - - [①](#ac21) 說“幾乎”是因為路徑并不會被放在列表的同一級上,盡管根據你的使用情況它可能并不重要。 [②](#ac22) Hy是Python上的Lisp實現,會在11.2節介紹。 [③](#ac23) 自Python 2.3版本實現的新導入鉤子機制。 [④](#ac24) 它在Python 3.3中又被加了回來。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看