从另一个.py文件导入函数不仅仅导入该函数?

时间:2021-02-14 16:00:31

To illustrate the issue I am having, please consider the following. I have two .py files, one named main.py and the other named testfuction.py. They are both in the same directory.

为了说明我遇到的问题,请考虑以下问题。我有两个.py文件,一个名为main.py,另一个名为testfuction.py。它们都在同一目录中。

The contents of main.py:

main.py的内容:

from testfunction import mytestfunction

mytestfunction()

The contents of testfunction.py:

testfunction.py的内容:

def mytestfunction():
    for number in range(0,10):
        print number

print "Hi"

I was under the impression that importing a function would only import that function. However, when I run main.py, this is what I get:

我的印象是导入函数只会导入该函数。但是,当我运行main.py时,这就是我得到的:

Hi
0
1
2
3
4
5
6
7
8
9
>>> 

Why is the print function being called? It isn't part of the function I imported!

为什么要调用打印功能?它不是我导入的功能的一部分!

3 个解决方案

#1


6  

I was under the impression that importing a function would only import that function.

我的印象是导入函数只会导入该函数。

You were incorrect.

你错了。

The first time a module is imported, an import statement will execute the entire module, including any global level print statements. Subsequent imports of the same module will re-use an existing module cached in sys.modules, which may be how you arrived at the misunderstanding that the entire module is not executed.

第一次导入模块时,import语句将执行整个模块,包括任何全局级别的print语句。后续导入的同一模块将重新使用sys.modules中缓存的现有模块,这可能是您如何解决整个模块未执行的误解。

Often you will find code which is not intended to be executed at import time to be located inside a conditional, like this:

通常,您会发现在导入时不打算执行的代码位于条件内,如下所示:

def mytestfunction():
    for number in range(0,10):
        print number

if __name__ == "__main__":
    print "Hi"

#2


3  

In order to import something from the module Python needs to load this module first. At that moment all the code at module-level is executed.

为了从模块导入一些东西,Python需要先加载这个模块。此时,执行模块级别的所有代码。

According to the docs:

根据文件:

A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement.

模块可以包含可执行语句以及函数定义。这些语句用于初始化模块。它们仅在第一次在import语句中遇到模块名时执行。

#3


3  

this question seems to be a duplicate of this one.

这个问题似乎与这一问题重复。

In short : all the code of a python file is called when importing the module. What is neither a function nor a class is usually put in a main function called here:

简而言之:导入模块时会调用python文件的所有代码。什么既不是函数也不是类通常放在这里调用的主函数中:

if __name__ == "__main__":
   # stuff only to run when not called via 'import' here
   main()

Please consider closing this thread.

请考虑关闭此主题。

#1


6  

I was under the impression that importing a function would only import that function.

我的印象是导入函数只会导入该函数。

You were incorrect.

你错了。

The first time a module is imported, an import statement will execute the entire module, including any global level print statements. Subsequent imports of the same module will re-use an existing module cached in sys.modules, which may be how you arrived at the misunderstanding that the entire module is not executed.

第一次导入模块时,import语句将执行整个模块,包括任何全局级别的print语句。后续导入的同一模块将重新使用sys.modules中缓存的现有模块,这可能是您如何解决整个模块未执行的误解。

Often you will find code which is not intended to be executed at import time to be located inside a conditional, like this:

通常,您会发现在导入时不打算执行的代码位于条件内,如下所示:

def mytestfunction():
    for number in range(0,10):
        print number

if __name__ == "__main__":
    print "Hi"

#2


3  

In order to import something from the module Python needs to load this module first. At that moment all the code at module-level is executed.

为了从模块导入一些东西,Python需要先加载这个模块。此时,执行模块级别的所有代码。

According to the docs:

根据文件:

A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement.

模块可以包含可执行语句以及函数定义。这些语句用于初始化模块。它们仅在第一次在import语句中遇到模块名时执行。

#3


3  

this question seems to be a duplicate of this one.

这个问题似乎与这一问题重复。

In short : all the code of a python file is called when importing the module. What is neither a function nor a class is usually put in a main function called here:

简而言之:导入模块时会调用python文件的所有代码。什么既不是函数也不是类通常放在这里调用的主函数中:

if __name__ == "__main__":
   # stuff only to run when not called via 'import' here
   main()

Please consider closing this thread.

请考虑关闭此主题。