📜  Python – 从另一个文件调用函数

📅  最后修改于: 2022-05-13 01:54:18.424000             🧑  作者: Mango

Python – 从另一个文件调用函数

给定一个Python文件,我们需要调用在任何其他Python文件中定义的函数。
例子:

方法:

  1. 创建一个包含所需函数的Python文件。
  2. 创建另一个Python文件并将之前的Python文件导入其中。
  3. 调用导入文件中定义的函数。

上述方法已在以下示例中使用:
示例 1:创建了一个Python文件 test.py,其中包含 displayText()函数。

Python3
# test.py>
 
# function
def displayText():
    print( "Geeks 4 Geeks !")


Python3
# importing  all the
# functions defined in test.py
from test import *
 
 
# calling functions
displayText()


Python3
# calc.py>
 
# functions
def addNumbers(a, b):
    print("Sum is ", a + b)
 
def subtractNumbers(a, b):
    print("Difference is ", a-b)
 
def multiplyNumbers(a, b):
    print("Product is ", a * b)
 
def divideNumbers(a, b):
    print("Division is ", a / b)
 
def modulusNumbers(a, b):
    print("Remainder is ", a % b)


Python3
# importing limited functions
# defined in calc.py
from calc import addNumbers, multiplyNumbers
 
 
# calling functions
addNumbers(2, 5)
multiplyNumbers(5, 4)


Python3
# test.py>
 
# function defined in test.py
def displayText():
    print("\nGeeks 4 Geeks !")


Python3
# calc.py>
 
# functions defined in calc.py
def addNumbers(a, b):
    print("Sum is ", a + b)
 
def subtractNumbers(a, b):
    print("Difference is ", a-b)
 
def multiplyNumbers(a, b):
    print("Product is ", a * b)
 
def divideNumbers(a, b):
    print("Division is ", a / b)
 
def modulusNumbers(a, b):
    print("Remainder is ", a % b)


Python3
# file.py>
 
# importing all the functions
# defined in calc.py
from calc import *
 
# importing required functions
# defined in test.py
from test import displayText
 
 
# calling functions defined
# in calc.py
addNumbers(25, 6)
subtractNumbers(25, 6)
multiplyNumbers(25, 6)
divideNumbers(25, 6)
modulusNumbers(25, 6)
 
# calling function defined
# in test.py
displayText()


现在创建了另一个Python文件,它调用 test.py 中定义的 displayText()函数。

Python3

# importing  all the
# functions defined in test.py
from test import *
 
 
# calling functions
displayText()

输出:

Geeks 4 Geeks!

在上面的程序中,将 test.py 文件中定义的所有函数都导入,然后调用一个函数。
示例 2:创建了一个Python文件 calc.py,其中包含 addNumbers()、subractNumbers()、multiplyNumbers()、divideNumbers() 和modalityNumbers()。

Python3

# calc.py>
 
# functions
def addNumbers(a, b):
    print("Sum is ", a + b)
 
def subtractNumbers(a, b):
    print("Difference is ", a-b)
 
def multiplyNumbers(a, b):
    print("Product is ", a * b)
 
def divideNumbers(a, b):
    print("Division is ", a / b)
 
def modulusNumbers(a, b):
    print("Remainder is ", a % b)

calc.py 中定义的函数在另一个Python文件中调用。

Python3

# importing limited functions
# defined in calc.py
from calc import addNumbers, multiplyNumbers
 
 
# calling functions
addNumbers(2, 5)
multiplyNumbers(5, 4)

输出:

7
20

在上面的程序中,所有在 calc.py 中定义的函数都没有被导入。
要导入Python文件中定义的所有函数:
句法:

from file import *

要仅导入Python文件中定义的必需函数:
句法:

from file import func1, func2, func3

示例 3:
以下Python文件 test.py 和 calc.py 是创建具有各种函数定义的。

Python3

# test.py>
 
# function defined in test.py
def displayText():
    print("\nGeeks 4 Geeks !")

Python3

# calc.py>
 
# functions defined in calc.py
def addNumbers(a, b):
    print("Sum is ", a + b)
 
def subtractNumbers(a, b):
    print("Difference is ", a-b)
 
def multiplyNumbers(a, b):
    print("Product is ", a * b)
 
def divideNumbers(a, b):
    print("Division is ", a / b)
 
def modulusNumbers(a, b):
    print("Remainder is ", a % b)

这两个文件都被导入到另一个名为 file.py 的Python文件中。

Python3

# file.py>
 
# importing all the functions
# defined in calc.py
from calc import *
 
# importing required functions
# defined in test.py
from test import displayText
 
 
# calling functions defined
# in calc.py
addNumbers(25, 6)
subtractNumbers(25, 6)
multiplyNumbers(25, 6)
divideNumbers(25, 6)
modulusNumbers(25, 6)
 
# calling function defined
# in test.py
displayText()

输出:

Sum is  31
Difference is  19
Product is  150
Division is  4.166666666666667
Remainder is  1

Geeks 4 Geeks!

在上面的程序中,在 test.py 和 calc.py 中定义的函数在不同的文件 file.py 中被调用。