名称空间的显式或一般导入? [英] Explicit or general importing of namespaces?
问题描述
在执行
之后是否真的有一个主要的性能差异:
导入Tkinter为TK
TK.Label(yada yada)
或
来自Tkinter进口的
*
标签(yada yada)
除了编写代码之外,我无法区分真正的区别
:-)。
谢谢,
Harlin
>在执行
编号之间确实存在重大的性能差异。
我无法区分真正的区别而不是代码编写
: - )。
不同之处在于from语法可能会导致名称空间污染。请参阅
常见问题解答:
http://www.python.org/doc/faq/programming.html#id12
-
问候,
Diez B. Roggisch
Diez B. Roggisch写道:[Harlin Seritt]:执行
$ b $之间确实存在重大的性能差异吗? b实际上,如果你在内循环中反复使用另一个模块中的名字
,那么如果你必须查找模块中的名字,它会产生很大的不同。反复。在这种情况下,只需明确
导入您想要重复使用的名称:
来自Tkinter import Label,otherfunction,othervariable
>
当然,当你在进行UI编程时,你不太可能看到这个用例
UI编程。
但是不要'' t do来自Tkinter import *因为Diez在常见问题解答中确定了
.
-
Michael Hoffman
Michael Hoffman写道:
但不要做来自Tkinter import *因为Diez已经在常见问题解答中找到了原因。
读取一些代码并且不得不从哪里猜到
do_complicated_stuff()函数是从中导入的。
明确导入到目前为止最好的莫斯科。
-
hilsen /问候丹麦Max M
http://www.mxm.dk/
IT的疯狂科学
Is there really a major performance difference between doing the
following:
import Tkinter as TK
TK.Label(yada yada)
OR
from Tkinter import *
Label(yada yada)
I''m unable to tell a real difference other than in the code writing
:-).
Thanks,
Harlin
解决方案> Is there really a major performance difference between doing the
No.
I''m unable to tell a real difference other than in the code writing
:-).
The difference is that the from-syntax may cause name space pollution. See
the FAQ:
http://www.python.org/doc/faq/programming.html#id12
--
Regards,
Diez B. Roggisch
Diez B. Roggisch wrote:[Harlin Seritt]:Is there really a major performance difference between doing the
No.
Actually, if you are using a name in another module repeatedly
in an inner loop, it can make a big difference if you have to lookup
the name in the module repeatedly. In that case, just explicitly
import the name you want to use repeatedly:
from Tkinter import Label, otherfunction, othervariable
Of course, you are not likely to see this use case when you''re doing
UI programming.
But don''t do "from Tkinter import *" for the reasons Diez has
identified in the FAQ.
--
Michael Hoffman
Michael Hoffman wrote:
But don''t do "from Tkinter import *" for the reasons Diez has
identified in the FAQ.
It is so annoying to read some code and having to guess from where the
do_complicated_stuff() function is imported from.
Explicit importing is by far the moste preferable.
--
hilsen/regards Max M, Denmark
http://www.mxm.dk/
IT''s Mad Science
这篇关于名称空间的显式或一般导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!