冗余进口 [英] redundant imports

查看:85
本文介绍了冗余进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


假设code-1.py导入code-2.py和code-3.py(因为它使用

名称来自两者),并且code-2.py导入code-3.py。


如果python是c,则代码-1c应该只包含* code-2.c ,因为

后者包括代码-3c。


在c中包含模块是纯粹的预处理文本事项

(编译推迟到事后),我猜,所以这样的事情是可能的。在python中导入模块是一个不同的野兽,

所以冗余是指(我认为)是必要的。


任何评论/建议/想法?


再见


macs

解决方案

max(01)*写道:

大家好。

假设code-1.py导入code-2.py和code-3.py(因为它使用了两者的名称),而code-2.py导入了code-3.py。如果python是c,则code-1.c应该只包含* code-2.c,因为
后者依次包含code-3.c。

在c中包含模块是纯粹的预处理文本事项
(编译推迟到事后),我想,这样的事情是可能的。在python中导入模块是一个不同的野兽,所以冗余是指冗余。是(我认为)必要的。

任何评论/建议/想法?

再见

macs




它看起来不像多余。一旦导入了一个模块,它就会进入sys.modules,并且任何后续的导入都会引用那个原始的导入,

所以只能读取和解析文件的开销一旦。您可能已经意识到,你可以通过* .pyc

文件来缓存编译结果;它只会编译导入的模块,如果它自

最后一次编译后发生了变化。


查看完全瘦的文档,特别是
http://www.python.org/doc/2.4/ ref / import.html


HTH,

Tim J


-

网址:www DOT jarmania FULLSTOP com


max(01)*写道:

大家好。

假设code-1.py导入code-2.py和code-3.py(因为它使用了两者的名称),而code-2.py导入了code-3.py。如果python是c,则code-1.c应该只包含* code-2.c,因为
后者依次包含code-3.c。

在c中包含模块是纯粹的预处理文本事项
(编译推迟到事后),我想,这样的事情是可能的。在python中导入模块是一个不同的野兽,所以冗余是指冗余。是(我认为)必要的。

任何意见/建议/想法?




你对这个想法感到困惑。


你首先说[code-1]使用两者的名字,

根据定义意味着它需要导入

两者。 (我的定义是因为导入是*如何*

a模块从另一个模块获取名称。)


然后你说代码-1可以选择不包括

code-3,因为其他一些模块包括它......

但是那又怎样?这并没有改变这样一个事实:

code-1需要来自code-3的名字,而且只是因为

code-3被导入其他地方是没有理由去思考

代码-1也神奇地得到它的名字。应该

所有模块都神奇地看到所有模块中的所有名称

哪些是导入的,甚至是其他模块?那个

几乎会击败

命名空间的大部分价值。


无论如何,为什么这种担心超过所谓的冗余

进口?源代码本身没有解析

并重新编译,甚至.pyc文件

也没有重新读取...一旦任何模块导入了

模块任何其他导入只是从sys.modules字典中检索到该模块的引用



这实际上是一个免费操作。 />

-Peter


Tim Jarman写道:

max(01)*写道:

大家好。

假设code-1.py导入code-2.py和code-3.py(因为它使用了
来自两者的名称,并且code-2.py导入code-3.py。
如果python是c,code-1.c应该只包含* code-2.c,因为
后者依次包含代码-3c。

在c中包含模块是一个纯粹的预处理文本事件
(编译推迟到事后),我想,这样的事情是可能的。在python中导入模块是一个不同的野兽,所以冗余是指冗余。是(我认为)必要的。

任何评论/建议/想法?



macs

它'这并不像它看起来那么多余。




这就是我使用引号的原因;-)

一旦模块被导入它进入sys.modules并且任何后续导入都引用了原始导入,因此读取和解析文件的开销只产生一次。您可能已经意识到,Python还会在* .pyc
文件中缓存编译结果;它只会编译导入的模块,如果它自上次编译后发生了变化。


这引出了另一个问题。因为* .pyc文件是第一次在给定的

模块上执行导入语句时自动创建
,我想如果我发送一个带有模块的程序用于一个

目录,其中用户没有写权限,那么我必须同时发送

* .pyc文件。对吗?

查看完全瘦的文档,特别是
http://www.python.org/doc/2.4/ref/import.html

HTH,
Tim J




非常感谢


再见


macs


hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it uses
names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

bye

macs

解决方案

max(01)* wrote:

hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it uses
names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

bye

macs



It''s not as redundant as it looks. Once a module has been imported it goes
into sys.modules and any subsequent imports refer to that original import,
so the overhead of reading and parsing the file is only incurred once. As
you''re probably aware, Python also caches compilation results in *.pyc
files; it will only compile the imported module if it changed since the
last compilation.

Check out the docs for the full skinny, in particular
http://www.python.org/doc/2.4/ref/import.html

HTH,
Tim J

--
Website: www DOT jarmania FULLSTOP com


max(01)* wrote:

hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it uses
names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?



You''re mixed up about this whole idea.

You say first that "[code-1] uses names from both",
which by definition means that it needs to import
both. (I say by definition because import is *how*
a module gets names from another module.)

Then you say that code-1 can choose not to include
code-3 because some other module is including it...
but so what? That doesn''t change the fact that
code-1 needs names from code-3, and just because
code-3 is imported elsewhere is no reason to think
that code-1 magically gets its names too. Should
all modules magically see all names in all modules
which are imported, even by other modules? That
would pretty much defeat most of the value of
namespaces.

Anyway, why this concern over so-called redundant
imports? The source code itself is not parsed
and compiled all over again, and even the .pyc file
is not re-read... once any module has imported a
module any other import just retrieves a reference
to that module from the sys.modules dictionary,
which is practically a free operation.

-Peter


Tim Jarman wrote:

max(01)* wrote:

hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it uses
names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

bye

macs

It''s not as redundant as it looks.



that''s why i used quotes ;-)
Once a module has been imported it goes
into sys.modules and any subsequent imports refer to that original import,
so the overhead of reading and parsing the file is only incurred once. As
you''re probably aware, Python also caches compilation results in *.pyc
files; it will only compile the imported module if it changed since the
last compilation.

this leads me to another question. since *.pyc files are automatically
created the first time an import statement in executed on a given
module, i guess that if i ship a program with modules for use in a
directory where the user has no write privileges then i must ship the
*.pyc files along too. right?
Check out the docs for the full skinny, in particular
http://www.python.org/doc/2.4/ref/import.html

HTH,
Tim J



thanks a lot

bye

macs


这篇关于冗余进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆