Python中使用语句导入模块或包的机制研究
这篇文章讨论了Python的from
从一个模块导入全部
from
# 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 <something.PublicClass object at ...> >>> c = _WeirdClass() ... NameError: name '_WeirdClass' is not defined
from something import *从something中导入了除了以_开头名称外的其他所有名称,按照规范,_开始的名称是私有的所以未被导入。
嗯,不是特别糟!还有什么?
上面没提到__all__是什么。__all__是一个字符串列表,指定了当from
# 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
现在,我们期望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 <something.PublicClass object at ...> >>> c = _WeirdClass() ... NameError: name '_WeirdClass' is not defined
包是怎样的呢?
当从一个包中导入全部时,__all__的做法和模块基本一样,不过它处理的是包中的模块(而不是把模块中的名都导入)。所以当我们使用from
不同之处在于,如果你在一个包的__init__.py里面没有声明__all__,from
但是,这有什么不好?
继续读之前,在你的Python解释器中,执行import this,再读一遍Python之禅(在你孩子每晚睡前也要读给他们)。
明确比含糊要好。
from
可读性很重要
即使你需要导入很多东西,一个一个显式地导入也更清楚。使用PEP 328:
from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text, LEFT, DISABLED, NORMAL, RIDGE, END)
你现在就能明确知道你的命名空间里有什么,使用ctrl+f能很快地告诉你它们是哪儿来的。
同时,你还总是要承担模块/包作者更改list内容(加/减东西)的风险。也就是下面两者之一:
作者从__all__里删除了一个字符串。如果你的代码使用了那个名字,你的代码就会报出NameError的错误,并且很难发现为什么。
作者在__all__里加入了很多东西。你也许不需要这些增加的内容,所以你只是让这些你不关心的东西占满了你的命名空间。他们甚至在你不注意的时候会替代其他同名内容。
当然,有时候从模块或者包中导入全部内容是有用的。不过,这么做之前三思。从我的经验来看,这么做通常只是因为懒。

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Quels sont les avantages et les inconvénients des modèles ?

Google AI annonce Gemini 1.5 Pro et Gemma 2 pour les développeurs

Partagez plusieurs frameworks de projets open source .NET liés à l'IA et au LLM

Comment enregistrer la fonction d'évaluation
