Đư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; Show
Approach:
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; Python3
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; Python37 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; Python3
7 208
from file import *1 from file import *2223 from file import *4 from file import *5
from file import *7
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
from file import func1, func2, func36
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
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!5
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
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; Python37 202 displayText(): 37 204 displayText(): 5
7 201
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; 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; 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; Python3
from file import *1 print 17 201 Python3
7 208
from file import *1 from file import *2223 from file import *4 from file import *5
from file import *7
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
from file import func1, func2, func36
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
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!5
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
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; Python37 202 displayText(): 37 204 displayText(): 5Đầu ra: & nbsp;
7 2037 displayText(): 87 2039 7 201 7 2041 7 2037 displayText(): 87 2039 7 201
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; |