Hướng dẫn how do you call a function from another program in python? - làm thế nào để bạn gọi một chức năng từ một chương trình khác trong python?

Nếu tệp của bạn nằm trong cấu trúc gói khác nhau và bạn muốn gọi nó từ một gói khác, thì bạn có thể gọi nó theo cách đó:

Giả sử bạn có cấu trúc gói sau trong dự án Python của mình:

Hướng dẫn how do you call a function from another program in python? - làm thế nào để bạn gọi một chức năng từ một chương trình khác trong python?

Trong -

from com.my.func.DifferentFunction import *
3 Tệp Python bạn có một số chức năng, như:

def add(arg1, arg2):
    return arg1 + arg2

def sub(arg1, arg2) :
    return arg1 - arg2

def mul(arg1, arg2) :
    return arg1 * arg2

Và bạn muốn gọi các chức năng khác nhau từ

from com.my.func.DifferentFunction import *
4, sau đó theo cách bạn có thể làm điều đó:

Xác định câu lệnh nhập trong

from com.my.func.DifferentFunction import *
4 - Tệp để nhập tất cả chức năng

from com.my.func.DifferentFunction import *

hoặc xác định từng tên chức năng mà bạn muốn nhập

from com.my.func.DifferentFunction import add, sub, mul

Sau đó, trong

from com.my.func.DifferentFunction import *
4, bạn có thể gọi chức năng để thực thi:

num1 = 20
num2 = 10

print("\n add : ", add(num1,num2))
print("\n sub : ", sub(num1,num2))
print("\n mul : ", mul(num1,num2))

Output:

 add :  30

 sub :  10

 mul :  200

Gọi các chức năng từ các tệp khác

Các chức năng do người dùng xác định có thể được gọi từ các tệp khác. Một hàm có thể được gọi và chạy trong một tệp khác với tệp nơi xác định hàm.

Nếu một tệp mới được gọi là myfunes.py được tạo và chứa hai định nghĩa hàm,

from com.my.func.DifferentFunction import *
7 và
from com.my.func.DifferentFunction import *
8, các hàm
from com.my.func.DifferentFunction import *
7 và
from com.my.func.DifferentFunction import *
8 có thể được sử dụng bởi một tập lệnh riêng biệt miễn là tên tệp và tên hàm được nhập trong tập lệnh riêng biệt trước tiên. Điều cần thiết là tệp chứa các định nghĩa hàm kết thúc trong phần mở rộng .py. Không có phần mở rộng .py, tệp nơi các chức năng được xác định không thể được nhập. Bên trong tệp myfuctions.py, hai chức năng được xác định bằng mã bên dưới.myfunctions.py is created and contains two function definitions,
from com.my.func.DifferentFunction import *
7 and
from com.my.func.DifferentFunction import *
8, the functions
from com.my.func.DifferentFunction import *
7 and
from com.my.func.DifferentFunction import *
8 can be used by a separate script as long as the file and function names are imported in the separate script first. It is essential that the file which contains the function definitions ends in the .py extension. Without a .py extension, the file where the functions are defined can not be imported. Inside the file myfuctions.py, two functions are defined using the code below.

# myfunctions.py

def plustwo(n):
    out = n + 2
    return out


def falldist(t,g=9.81):
    d = 0.5 * g * t**2
    return d

Tệp này, myfunes.py có thể được nhập vào một tập lệnh khác (một tệp .py khác) hoặc máy tính xách tay Jupyter.myfunctions.py can be imported into another script (another .py file), or Jupyter Notebook.

Hãy nhớ rằng tệp chứa các định nghĩa chức năng và tệp gọi các hàm phải nằm trong cùng một thư mục.

Để sử dụng các chức năng được viết trong một tệp bên trong một tệp khác bao gồm dòng nhập,

from com.my.func.DifferentFunction import add, sub, mul
1. Lưu ý rằng mặc dù tên tệp phải chứa phần mở rộng .py,
from com.my.func.DifferentFunction import add, sub, mul
2 không được sử dụng như một phần của tên tệp trong quá trình nhập..py extension,
from com.my.func.DifferentFunction import add, sub, mul
2 is not used as part of the filename during import.

Cú pháp chung để nhập và gọi hàm từ một tệp riêng biệt ở bên dưới:

from function_file import function_name

function_name(arguments)

Một ví dụ sử dụng cú pháp này với tệp myfifts.py và hàm

from com.my.func.DifferentFunction import *
7 ở bên dưới:myfunctions.py file and the function
from com.my.func.DifferentFunction import *
7 is below:

In [1]:

from myfunctions import plustwo

plustwo(3)

Nhiều chức năng có thể được nhập từ cùng một tệp bằng cách tách các chức năng được nhập với dấu phẩy. Cú pháp chung để nhập và gọi nhiều chức năng từ cùng một tệp là bên dưới:
from function_file import function_name1, function_name2

function_name1(arguments)
function_name2(arguments)

Một ví dụ sử dụng cú pháp này với tệp myfifts.py và các chức năng

from com.my.func.DifferentFunction import *
7 và
from com.my.func.DifferentFunction import *
8 dưới đây:myfunctions.py file and the functions
from com.my.func.DifferentFunction import *
7 and
from com.my.func.DifferentFunction import *
8 is below:

In [2]:

from myfunctions import falldist, plustwo

out1 = falldist(3) out2 = plustwo(3)

print(out1, out2)

Một cách khác để nhập và sử dụng các chức năng từ myFunctions.py vào một tập lệnh khác hoặc máy tính xách tay Jupyter là nhập toàn bộ tệp myfifts.py với
from com.my.func.DifferentFunction import add, sub, mul
6, sau đó gọi các chức năng với cú pháp bên dưới.
from com.my.func.DifferentFunction import *
0myfunctions.py into another script or Jupyter notebook is to import the entire myfunctions.py file with
from com.my.func.DifferentFunction import add, sub, mul
6, then call the functions with the syntax below.
from com.my.func.DifferentFunction import *
0

Một ví dụ sử dụng cú pháp này với tệp myfifts.py bên dưới.myfunctions.py file is below.

In [3]:

from com.my.func.DifferentFunction import *
1

In [4]:

from com.my.func.DifferentFunction import *
2