简体   繁体   English

我在从其他文件调用 function 时遇到问题

[英]I'm having a problem calling a function from a different file

from practicing import sort_books_publisher
sort_books_publisher()

When I call the function from another file every function in that file runs, please what could be the problem and what do I do if I want to call this function by pressing a Letter?当我从另一个文件中调用 function 时,该文件中的每个 function 运行时,请问可能是什么问题,如果我想通过按字母来调用这个 ZC1C425268E68385D1AB5074C17A94F,我该怎么办?

Do you mean that every function from practicing.py runs, or that the code in the main part of practicing.py runs when you import the module?你的意思是每个practicing.py从practice.py 运行,还是practicing.py你导入模块时运行practice.py 的主要部分的代码?

If everything in the external module is wrapped inside function definitions, importing the module shouldn't have any effect on what you're doing in the main program.如果外部模块中的所有内容都包含在 function 定义中,那么导入模块应该不会对您在主程序中所做的事情产生任何影响。

If you still want code that is in the main part of the external module to be executable by running that file, the thing to do is wrap the main part of the module in a function def main(): and put the following in the main part of the module (this will not run if the module is imported, only if it is run directly):如果您仍然希望外部模块的主要部分中的代码可以通过运行该文件来执行,那么要做的是将模块的主要部分包装在 function def main():中并将以下内容放在 main模块的一部分(如果模块被导入,这将不会运行,只有在直接运行时才会运行):

if __name__ == "__main__":
    main()

I can't see entire of code.我看不到整个代码。

But, I think you have to know how to utilize the argument of the function.但是,我认为您必须知道如何利用 function 的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM