14
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Làm thế nào để bạn gọi một hàm từ một tệp đã nhập? Ví dụ:
Bài kiểm tra:
import test2
def aFunction[]:
print "hi there"
Test2:
import test
aFunction[]
Điều này cho tôi một lỗi tên, nói rằng chức năng của tôi không được xác định. Tôi cũng đã thử:
from test import aFunction
And:
from test import *
Tôi cũng đã thử không nhập Test2 trong thử nghiệm. Tôi đang đến Python từ C ++, vì vậy tôi sợ rằng tôi đang thiếu một thứ gì đó rõ ràng rõ ràng đối với các progammer Python kỳ cựu ...
Đã hỏi ngày 27 tháng 9 năm 2013 lúc 16:58Sep 27, 2013 at 16:58
3
Bạn đang tạo một nhập khẩu tròn.
import test
aFunction[]
0 Nhập khẩu import test
aFunction[]
1 cố gắng nhập import test
aFunction[]
0.Đừng làm điều này. Vào thời điểm
import test
aFunction[]
3 nhập import test
aFunction[]
4, mô -đun đó chưa hoàn thành việc thực thi tất cả các mã; Chức năng chưa được xác định:
4 được biên dịch và thực thi, và một đối tượng mô -đun trống được thêm vàoimport test aFunction[]
6.import test aFunction[]
Dòng
7 được chạy.import test aFunction[]
3 được biên dịch và thực thi, và một đối tượng mô -đun trống được thêm vàoimport test aFunction[]
6.import test aFunction[]
Dòng
0 được chạy.from test import aFunction
4 đã có mặt dưới dạng mô -đun trongimport test aFunction[]
6, đối tượng này được trả về và liên kết với tênimport test aFunction[]
4.import test aFunction[]
Một dòng tiếp theo cố gắng chạy
4. Không có tên như vậy tồn tại trongfrom test import aFunction
4. Một ngoại lệ được nâng lên.import test aFunction[]
Các dòng xác định
6 không bao giờ được thực thi, bởi vì một ngoại lệ đã được nêu ra.from test import aFunction
Xóa dòng
import test
aFunction[]
7 và chạy trực tiếp import test
aFunction[]
1 và nhập chức năng sẽ hoạt động tốt:import test
test.aFunction[]
Đã trả lời ngày 27 tháng 9 năm 2013 lúc 17:01Sep 27, 2013 at 17:01
Martijn Pieters ♦ Martijn Pieters♦Martijn Pieters
991K276 Huy hiệu vàng3905 Huy hiệu bạc3254 Huy hiệu đồng276 gold badges3905 silver badges3254 bronze badges
12
Đư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
from test import aFunction
9 from test import *
0from test import *
1from test import *
2from test import *
3from test import *
4from test import *
5Bâ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
from test import *
6 from test import *
7from test import *
8 from test import *
9import test
test.aFunction[]
0Output:
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
from test import aFunction
9 import test
test.aFunction[]
2from test import *
1from test import *
2import test
test.aFunction[]
5import test
test.aFunction[]
6import test
test.aFunction[]
7import test
test.aFunction[]
8 import test
test.aFunction[]
9from test import aFunction
9 Geeks 4 Geeks!1
from test import *
1from test import *
2import test
test.aFunction[]
5Geeks 4 Geeks!5
Geeks 4 Geeks!6
Geeks 4 Geeks!7
import test
test.aFunction[]
9from test import aFunction
9 7 200
Các
from test import aFunction
9 7 209
from test import *
1from test import *
2import test
test.aFunction[]
5from file import *3
import test
test.aFunction[]
7from file import *5
from test import aFunction
9 from file import *8
from test import *
1from test import *
2import test
test.aFunction[]
5from file import func1, func2, func32
import test
test.aFunction[]
7from file import func1, func2, func34
import test
test.aFunction[]
9Các hàm được xác định trong calc.py được gọi trong một tệp python khác. & Nbsp;
Python3
from test import *
6 from file import func1, func2, func37
from test import *
8 from file import func1, func2, func39
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!0
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!1
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!2
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!3
from test import *
5Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!5
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!3
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!2
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!8
from test import *
5Đầ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
from test import aFunction
9 from test import *
0from test import *
1from test import *
2import test
test.aFunction[]
5import test
aFunction[]
05from test import *
5Python3
from test import aFunction
9 import test
test.aFunction[]
2from test import *
1from test import *
2import test
test.aFunction[]
5import test
test.aFunction[]
6import test
test.aFunction[]
7import test
test.aFunction[]
8 import test
test.aFunction[]
9from test import aFunction
9 Geeks 4 Geeks!1
from test import *
1from test import *
2import test
test.aFunction[]
5Geeks 4 Geeks!5
Geeks 4 Geeks!6
Geeks 4 Geeks!7
import test
test.aFunction[]
9from test import aFunction
9 7 200
Các
from test import aFunction
9 7 209
from test import *
1from test import *
2import test
test.aFunction[]
5from file import *3
import test
test.aFunction[]
7from file import *5
from test import aFunction
9 from file import *8
from test import *
1from test import *
2import test
test.aFunction[]
5from file import func1, func2, func32
import test
test.aFunction[]
7from file import func1, func2, func34
import test
test.aFunction[]
9Các hàm được xác định trong calc.py được gọi trong một tệp python khác. & Nbsp;
Python3
from test import *
6 from file import func1, func2, func37
from test import *
8 from file import func1, func2, func39
from test import *
6 from test import *
7from test import *
8 import test
aFunction[]
59Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!0
import test
aFunction[]
61Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!2
import test
aFunction[]
63from test import *
5import test
aFunction[]
65import test
aFunction[]
61Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!2
import test
aFunction[]
63from test import *
5Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!5
import test
aFunction[]
61Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!2
import test
aFunction[]
63from test import *
5import test
aFunction[]
75import test
aFunction[]
61Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!2
import test
aFunction[]
63from test import *
5import test
aFunction[]
80import test
aFunction[]
61Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!2
import test
aFunction[]
63from test import *
5import test
test.aFunction[]
0Output:
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!
Trong chương trình trên, các chức năng được xác định trong Test.Py và calc.py được gọi trong một tệp khác là File.py. & nbsp;