从导入文件中的函数调用函数

时间:2022-12-07 23:59:51

I have a file, let's call it One.py:

我有一个文件,我们称之为One.py:

from Two.py import example2

def example2():
   print "something!"

and another file named Two.py:

和另一个名为Two.py的文件:

def example1():
   print "Other thing!"
   example2()

However, on the bottom line when I try to call example2() it doesn't recognize it. I would like to avoid simply importing One.py into Two.py because this would create an import loop.

但是,在我试图调用example2()的底线时,它无法识别它。我想避免简单地将One.py导入Two.py,因为这会创建一个导入循环。

2 个解决方案

#1


First of all, you import by simply naming the module, without the file ending:

首先,只需命名模块即可导入,文件不会结束:

from Two import example2

This should horribly fail, as there is no definition of example2 in Two.py

这应该可怕地失败,因为在Two.py中没有example2的定义

A simple example to do what I guess you want looks like that:

做我想你想做的一个简单的例子看起来像这样:

One.py:

def function1():
    print 'I am function 1'

Two.py:

from One import function1
def function2():
    print 'I am function 2'
function1()
function2()

#2


As the comments stated, your code doesn't make too much sense. Let's look at your Two.py file:

正如评论所述,您的代码没有多大意义。我们来看看你的Two.py文件:

#Two.py
def example1():
print "Other thing!"
example2()

The file containts the definition for a function named example1, which prints a string. When executed, example2() will never get called because example2() was never declared anywhere. If you declare it locally, you will need to add a function in Two.py that defines it. For example, append the following lines of code to your Two.py file:

该文件包含名为example1的函数的定义,该函数打印字符串。执行时,example2()将永远不会被调用,因为example2()从未在任何地方声明过。如果在本地声明它,则需要在Two.py中添加一个定义它的函数。例如,将以下代码行附加到Two.py文件中:

def example2():
    print "Example 2 was called"

Now, when you try to call the function, the compiler will know what you want it to do because you declared it locally. If, on the other hand, you did not declare the function in the same file but have defined it elsewhere, you can import the function from another file. Since One.py has a definition for example2(), you can import the module in your Two.py to gain access to it. Your final two files should look like this:

现在,当您尝试调用该函数时,编译器将知道您希望它做什么,因为您在本地声明了它。另一方面,如果您没有在同一文件中声明该函数但在其他位置已将其定义,则可以从另一个文件导入该函数。由于One.py具有example2()的定义,因此您可以导入Two.py中的模块以获取对它的访问权限。您的最后两个文件应如下所示:

#Two.py
from One.py import example2  #instead of importing it from elsewhere, you can also define it in the actual file
def example1():
    print "Other thing!"     #notice the indentation that you left out
example1()
example2()

#One.py
def example2():
    print "something!"

This will execute without error.

这将毫无错误地执行。

To understand what went wrong with your implementation, consider the following line of code:

要了解您的实现出了什么问题,请考虑以下代码行:

from Two.py import example2

The above states that there is some file called Two.py and that it contains a definition for a function called example2. But if you take a close look at your Two.py file, you can see that there was no example2 to import. Thus, the compiler issues a warning telling you something is wrong.

上面说明有一些名为Two.py的文件,它包含一个名为example2的函数的定义。但是如果你仔细查看你的Two.py文件,你会发现没有要导入的example2。因此,编译器发出警告,告诉您出错。

#1


First of all, you import by simply naming the module, without the file ending:

首先,只需命名模块即可导入,文件不会结束:

from Two import example2

This should horribly fail, as there is no definition of example2 in Two.py

这应该可怕地失败,因为在Two.py中没有example2的定义

A simple example to do what I guess you want looks like that:

做我想你想做的一个简单的例子看起来像这样:

One.py:

def function1():
    print 'I am function 1'

Two.py:

from One import function1
def function2():
    print 'I am function 2'
function1()
function2()

#2


As the comments stated, your code doesn't make too much sense. Let's look at your Two.py file:

正如评论所述,您的代码没有多大意义。我们来看看你的Two.py文件:

#Two.py
def example1():
print "Other thing!"
example2()

The file containts the definition for a function named example1, which prints a string. When executed, example2() will never get called because example2() was never declared anywhere. If you declare it locally, you will need to add a function in Two.py that defines it. For example, append the following lines of code to your Two.py file:

该文件包含名为example1的函数的定义,该函数打印字符串。执行时,example2()将永远不会被调用,因为example2()从未在任何地方声明过。如果在本地声明它,则需要在Two.py中添加一个定义它的函数。例如,将以下代码行附加到Two.py文件中:

def example2():
    print "Example 2 was called"

Now, when you try to call the function, the compiler will know what you want it to do because you declared it locally. If, on the other hand, you did not declare the function in the same file but have defined it elsewhere, you can import the function from another file. Since One.py has a definition for example2(), you can import the module in your Two.py to gain access to it. Your final two files should look like this:

现在,当您尝试调用该函数时,编译器将知道您希望它做什么,因为您在本地声明了它。另一方面,如果您没有在同一文件中声明该函数但在其他位置已将其定义,则可以从另一个文件导入该函数。由于One.py具有example2()的定义,因此您可以导入Two.py中的模块以获取对它的访问权限。您的最后两个文件应如下所示:

#Two.py
from One.py import example2  #instead of importing it from elsewhere, you can also define it in the actual file
def example1():
    print "Other thing!"     #notice the indentation that you left out
example1()
example2()

#One.py
def example2():
    print "something!"

This will execute without error.

这将毫无错误地执行。

To understand what went wrong with your implementation, consider the following line of code:

要了解您的实现出了什么问题,请考虑以下代码行:

from Two.py import example2

The above states that there is some file called Two.py and that it contains a definition for a function called example2. But if you take a close look at your Two.py file, you can see that there was no example2 to import. Thus, the compiler issues a warning telling you something is wrong.

上面说明有一些名为Two.py的文件,它包含一个名为example2的函数的定义。但是如果你仔细查看你的Two.py文件,你会发现没有要导入的example2。因此,编译器发出警告,告诉您出错。