Python中使用语句导入模块或包的机制研究

  这篇文章讨论了Python的from import *和from import *,它们怎么执行以及为什么使用这种语法。

  从一个模块导入全部

  from import * means意味着“我希望能访问中我有权限访问的全部名称”。例如以下代码something.py:

  # something.py

  public_variable = 42

  _private_variable = 141

  def public_function():

  print("I'm a public function! yay!")

  def _private_function():

  print("Ain't nobody accessing me from another module...usually")

  class PublicClass(object):

  pass

  class _WeirdClass(object):

  pass

  在Python解释器中,我们可以执行from something import *,然后看到如下的内容:

  >>> from something import *

  >>> public_variable

  42

  >>> _private_variable

  ...

  NameError: name '_private_variable' is not defined

  >>> public_function()

  "I'm a public function! yay!"

  >>> _private_function()

  ...

  NameError: name '_private_function' is not defined

  >>> c = PublicClass()

  >>> c

  >>> c = _WeirdClass()

  ...

  NameError: name '_WeirdClass' is not defined

  from something import *从something中导入了除了以_开头名称外的其他所有名称,按照规范,_开始的名称是私有的所以未被导入。

  上面没提到__all__是什么。all__是一个字符串列表,指定了当from import *被使用时,模块(或者如后文会提到的包)中的哪些符号会被导出。如果我们不定义__all(我们在上面的something.py就没定义),import *默认的导入方式是导入除了下划线(_)开头的所有名称。再说一次,编程惯例上下划线表示一个符号是私有的,不导入是合理的。让我们来看看在something.py中定义我们自己的__all__会发生什么。

  # something.py

  __all__ = ['_private_variable', 'PublicClass']

  # The rest is the same as before

  public_variable = 42

  _private_variable = 141

  def public_function():

  print("I'm a public function! yay!")

  def _private_function():

  print("Ain't nobody accessing me from another module...usually")

  class PublicClass(object):

  pass

  class _WeirdClass(object):

  pass郑州做人流多少钱 http://mobile.120zzzzyy.com/

  现在,我们期望from something import *只会导入_private_variable和PublicClass:

  >>> from something import *

  >>> public_variable

  42

  >>> _private_variable

  ...

  NameError: name '_private_variable' is not defined

  >>> public_function()

  "I'm a public function! yay!"

  >>> _private_function()

  ...

  NameError: name '_private_function' is not defined

  >>> c = PublicClass()

  >>> c

  >>> c = _WeirdClass()

  ...

  NameError: name '_WeirdClass' is not defined

  包是怎样的呢?

  当从一个包中导入全部时,__all__的做法和模块基本一样,不过它处理的是包中的模块(而不是把模块中的名都导入)。所以当我们使用from import *.时__all__说明了所有需要被导入当前命名空间的模块。

  不同之处在于,如果你在一个包的__init__.py里面没有声明__all__,from import *语句不会导入任何东西(这个说法也不全对,正确的说法在此)

  from import * 是不明确的。它没告诉我们我们正在导入什么或者我们把什么带入当前命名空间了。更好的做法是显式地导入我们需要的全部名称。这种方式下,读者(非常可能是未来的你自己)就不会困惑于你代码中使用的一个变量/方法/类/其他东西是哪儿来的,这也告诉了我们下一点:

  可读性很重要

  即使你需要导入很多东西,一个一个显式地导入也更清楚。使用PEP 328:

  from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,

  LEFT, DISABLED, NORMAL, RIDGE, END)

  你现在就能明确知道你的命名空间里有什么,使用ctrl+f能很快地告诉你它们是哪儿来的。

  同时,你还总是要承担模块/包作者更改list内容(加/减东西)的风险。也就是下面两者之一:

  作者从__all__里删除了一个字符串。如果你的代码使用了那个名字,你的代码就会报出NameError的错误,并且很难发现为什么。

  作者在__all__里加入了很多东西。你也许不需要这些增加的内容,所以你只是让这些你不关心的东西占满了你的命名空间。他们甚至在你不注意的时候会替代其他同名内容。

  当然,有时候从模块或者包中导入全部内容是有用的。

posted @ 2020-03-06 10:47  网管布吉岛  阅读(253)  评论(0编辑  收藏  举报