Đưa ra một tệp python, chúng ta cần gọi một hàm trong nó được xác định trong bất kỳ tệp python nào khác. & Nbsp; ví dụ: & nbsp; & nbsp;
Example:
Giả sử có một test test.py chứa định nghĩa của hàm displaytext []. & Nbsp;#test.py> def displayText []: Chúng ta cần gọi hàm displayText [] trong bất kỳ tệp python nào khác sao cho bất cứ nơi nào chúng ta gọi là displayText [] Hiển thị văn bản có trong đó. Điều này có thể được thực hiện bằng các mô -đun Python. & NBSP; & nbsp;
#test.py>
def displayText[]:
print[ “Geeks 4 Geeks!”]
We need to call the function displayText[] in any other Python file such that wherever we call displayText[] function displays text present in it. This can be done using Python modules.
Approach:
- Tạo một tệp Python chứa các chức năng cần thiết.
- Tạo một tệp Python khác và nhập tệp Python trước đó vào đó.
- Gọi các chức năng được xác định trong tệp đã nhập.
Cách tiếp cận trên đã được sử dụng trong các ví dụ dưới đây: Ví dụ 1: một thử nghiệm tệp Python được tạo và nó chứa hàm displaytext []. & Nbsp;
Example 1: A Python file test.py is created and it contains the displayText[]
function.
Python3
def
displayText[]:
print
[
7 200
7 201
Bây giờ một tệp python khác được tạo, gọi hàm displayText [] được xác định trong test.py. & nbsp;
Python3
7 202
7 203
7 204
7 205
7 206
Output:
Geeks 4 Geeks!
Trong chương trình trên, tất cả các hàm được xác định trong tệp Test.py được nhập thì một hàm được gọi là modulusnumbers []. & nbsp;
Example 2: A Python file calc.py is created containing addNumbers[], subractNumbers[], multiplyNumbers[], divideNumbers[] and modulusNumbers[].
Python3
def
7 208
print
from file import *1
from file import *2223
from file import *4
from file import *5
def
from file import *7
print
from file import *1
from file import func1, func2, func31
from file import func1, func2, func32
from file import func1, func2, func33
from file import *5
def
from file import func1, func2, func36
print
from file import *1
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!0
from file import *3
7 205
def
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!5
print
from file import *1
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!9
from file import *3
def
1 from file import *5
def
def
4
print
from file import *1
def
8from file import *3
displayText[]:
0Các hàm được xác định trong calc.py được gọi trong một tệp python khác. & Nbsp;
Python3
7 202
displayText[]:
37 204
displayText[]:
5displayText[]:
6displayText[]:
7displayText[]:
8displayText[]:
9
7 201
1displayText[]:
9displayText[]:
8
4
7 201
Đầu ra: & nbsp;
7 20
Trong chương trình trên, tất cả các chức năng được xác định trong calc.py không được nhập. & Nbsp; để nhập tất cả các chức năng được xác định trong tệp python: cú pháp: & nbsp;
To import all the functions defined in a Python file:
Syntax:
from file import *
Để chỉ nhập các hàm yêu cầu được xác định trong tệp Python: Cú pháp: & NBSP;
Syntax:
from file import func1, func2, func3
Ví dụ 3: & nbsp; Test Python Test.Py và calc.py bên dưới được tạo có các định nghĩa chức năng khác nhau. & NBSP;
The below Python files test.py and calc.py are created having various function definitions.
Python3
def
displayText[]:
print
from file import *1
print
17 201
Python3
def
7 208
print
from file import *1
from file import *2223
from file import *4
from file import *5
def
from file import *7
print
from file import *1
from file import func1, func2, func31
from file import func1, func2, func32
from file import func1, func2, func33
from file import *5
def
from file import func1, func2, func36
print
from file import *1
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!0
from file import *3
7 205
def
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!5
print
from file import *1
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!9
from file import *3
def
1 from file import *5
def
def
4
print
from file import *1
def
8from file import *3
displayText[]:
0Các hàm được xác định trong calc.py được gọi trong một tệp python khác. & Nbsp;
Python3
7 202
displayText[]:
37 204
displayText[]:
5Đầu ra: & nbsp;
displayText[]:
6
7 2037
displayText[]:
87 2039
7 201
7 2041
7 2037
displayText[]:
87 2039
7 201
1
7 2037
displayText[]:
87 2039
7 201
Trong chương trình trên, tất cả các chức năng được xác định trong calc.py không được nhập. & Nbsp; để nhập tất cả các chức năng được xác định trong tệp python: cú pháp: & nbsp;
7 2056
7 2037
displayText[]:
87 2039
7 201
7 206
Output:
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!
Để chỉ nhập các hàm yêu cầu được xác định trong tệp Python: Cú pháp: & NBSP;