Python – Memanggil fungsi dari file lain
Meningkatkan Artikel
Simpan Artikel
Seperti Artikel
- Tingkat Kesulitan. Mudah
- Terakhir Diperbarui. 21 Mei, 2021
Meningkatkan Artikel
Simpan Artikel
Diberikan file Python, kita perlu memanggil fungsi di dalamnya yang ditentukan dalam file Python lainnya.
Contoh.
Misalkan ada file test. py yang berisi definisi fungsi displayText[].
#tes. py>
def tampilanTeks[].
print[ “Geek 4 Geek. ”]
Kita perlu memanggil fungsi displayText[] di file Python lain sehingga di mana pun kita memanggil fungsi displayText[] menampilkan teks yang ada di dalamnya. Ini dapat dilakukan dengan menggunakan modul Python.
Mendekati
- Buat file Python yang berisi fungsi yang diperlukan
- Buat file Python lain dan impor file Python sebelumnya ke dalamnya
- Panggil fungsi yang ditentukan dalam file yang diimpor
Pendekatan di atas telah digunakan dalam contoh di bawah ini
Contoh 1. Tes file Python. py dibuat dan berisi fungsi displayText[]
Python3
# test.py>
# function
def
displayText[]:
7 200
7 201
7 202
7 203
Sekarang file Python lain dibuat yang memanggil fungsi displayText[] yang ditentukan dalam test. py
Python3
7 20_4
7 205
7 206
7 207
7 208
7 209
from file import *_0
from file import *_1
Keluaran
Geeks 4 Geeks!
Dalam program di atas, semua fungsi didefinisikan dalam test. py file diimpor kemudian fungsi dipanggil
Contoh 2. Kalkulasi file Python. py dibuat berisi addNumbers[], subractNumbers[], multiplyNumbers[], divideNumbers[] dan modulusNumbers[]
Python3
from file import *_2
from file import *_3
def
from file import *5
7 200
7 201
from file import *9
from file import func1, func2, func30
from file import func1, func2, func31
from file import func1, func2, func32
def
from file import func1, func2, func34
7 200
7 201
from file import func1, func2, func38
from file import func1, func2, func30
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!0
from file import func1, func2, func32
def
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!3
7 200
7 201
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!7
from file import func1, func2, func30________0______9
from file import func1, func2, func32
def
# test.py>
2
7 200
7 201
# test.py>
6from file import func1, func2, func30
# test.py>
8 from file import func1, func2, func32
def
# function
1
7 200
7 201
# function
5from file import func1, func2, func30
# function
7 from file import func1, func2, func32
Fungsi yang didefinisikan dalam calc. py dipanggil dalam file Python lain
Python3
# function
_9
def
_0
7 206
def
27 208
def
4
from file import *_0
________122______6def
7def
8def
9
7 203
displayText[]:
1def
9def
8displayText[]:
4
7 203
Keluaran
7 20
Dalam program di atas, semua fungsi yang didefinisikan dalam calc. py tidak diimpor.
Untuk mengimpor semua fungsi yang didefinisikan dalam file Python
Sintaksis.
from file import *
Untuk mengimpor hanya diperlukan fungsi yang ditentukan dalam file Python
Sintaksis
from file import func1, func2, func3
Contoh 3.
Tes file Python di bawah ini. py dan kal. py dibuat dengan berbagai definisi fungsi
Python3
# test.py>
displayText[]:
_7
def
displayText[]:
7 200
7 201
37 203
Python3
from file import *_2
_6
def
from file import *5
7 200
7 201
from file import *9
from file import func1, func2, func30
from file import func1, func2, func31
from file import func1, func2, func32
def
from file import func1, func2, func34
7 200
7 201
from file import func1, func2, func38
from file import func1, func2, func30
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!0
from file import func1, func2, func32
def
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!3
7 200
7 201
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!7
from file import func1, func2, func30________0______9
from file import func1, func2, func32
def
# test.py>
2
7 200
7 201
# test.py>
6from file import func1, func2, func30
# test.py>
8 from file import func1, func2, func32
def
# function
1
7 200
7 201
# function
5from file import func1, func2, func30
# function
7 from file import func1, func2, func32
Kedua file diimpor ke file bernama file Python lain. py
Python3
7 20_42
7 20_43
def
_0
7 206
def
27 208
7 209
7 20_49
7 20_50
7 206
7 207
7 208
7 2054
7 20_55
7 20_56
________122______6
7 2058
def
87 2060
7 203
7 2062
7 2058
def
87 2060
7 203
displayText[]:
1
7 2058
def
87 2060
7 203
7 2072
7 2058
def
87 2060
7 203
7 2077
7 2058
def
87 2060
7 203
7 20_82
7 20_83
from file import *_1
Keluaran
Sum is 31 Difference is 19 Product is 150 Division is 4.166666666666667 Remainder is 1 Geeks 4 Geeks!
Dalam program di atas, fungsi didefinisikan dalam test. py dan kal. py dipanggil dalam file berbeda yaitu file. py
Catatan Pribadi Saya arrow_drop_up
Menyimpan