Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Trong bài viết này, chúng ta sẽ xem làm thế nào để gán một hàm cho một biến trong Python. Trong Python, chúng ta có thể gán một hàm cho một biến. Và sử dụng biến đó, chúng tôi có thể gọi hàm nhiều như thời gian chúng tôi muốn. Do đó, tăng khả năng tái sử dụng mã. & Nbsp;
Syntax::
def func[]: { .. } var=func var[] var[]
Example:
Python3
Thực hiện
GFG2
GFG3
GFG4
GFG5
GFG6
Chỉ cần gán một hàm cho biến mong muốn nhưng không có [] tức là chỉ với tên của hàm. Nếu biến được gán với hàm cùng với dấu ngoặc [], sẽ không được trả về.
123 98 123 98 1230
Output:
GFG
GFG0
GFG1
GFG
7GFG
8GFG
9
Python3
Các chương trình sau đây sẽ giúp bạn hiểu rõ hơn:
Ví dụ 1: & nbsp;
123 98 123 98 1231
GFG8
123 98 123 98 1233
123 98 123 98 1237
GFG3
odd number even number odd number3
123 98 123 98 1237
GFG3
GFG4
odd number even number odd number7
odd number even number odd number8
odd number even number odd number9
240 400 40000
GFG3
odd number even number odd number3
GFG0
123 98 123 98 1235
123 98 123 98 1236
240 400 40006
123 98 123 98 1237
123 98 123 98 1231
GFG8
odd number even number odd number0
Output:
123 98 123 98 123
240
400
4000
3GFG
8 123
98
123
98
123
5parameterized function
Python3
240 400 40006
Ví dụ 2: Hàm được tham số hóa
total = add[1, 4]
#total = 5
9GFG3
GFG4
add[1, 4] = total
2GFG6
123 98 123 98 1237
add[1, 4] = total
5total = add[1, 4]
#total = 5
8total = add[1, 4]
#total = 5
9GFG3
GFG4
invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
0GFG6
GFG0
240 400 40009
invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
5invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
6GFG6
invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
5invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
9GFG6
123 98 123 98 1237
total = add[1, 4]
#total = 5
1 total = add[1, 4]
#total = 5
22____53 total = add[1, 4]
#total = 5
4 GFG8
GFG8
Output:
odd number even number odd number
240
400
4000
3GFG
8
4invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
Python3
invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
5subsequent_amount = invest[initial_amount,top_company[5,year,year+1]]
22Ví dụ 3:
GFG0
subsequent_amount = invest[initial_amount,top_company[5,year,year+1]]
5123 98 123 98 1237
subsequent_amount = invest[initial_amount,top_company[5,year,year+1]]
7GFG8
subsequent_amount = invest[initial_amount,top_company[5,year,year+1]]
9123 98 123 98 1237
invest[initial_amount,top_company[5,year,year+1]] == subsequent_amount
1GFG8
GFG9
invest[initial_amount,top_company[5,year,year+1]] == subsequent_amount
4invest[initial_amount,top_company[5,year,year+1]] == subsequent_amount
5GFG3
GFG03
GFG04
GFG05
GFG3
GFG03
invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
9GFG05
GFG3
GFG03
GFG12
GFG05
Output:
240 400 4000
Trong Python, nếu chúng ta đặt dấu ngoặc đơn sau một tên hàm, ví dụ,
GFG14, điều này chỉ ra một lệnh gọi hàm và giá trị của nó tương đương với giá trị được trả về bởi hàm
GFG14.
Tuyên bố gọi chức năng được cho là để có được một giá trị. Ví dụ:
total = add[1, 4]
#total = 5
Và nếu chúng tôi cố gắng gán một giá trị cho câu lệnh Gọi hàm trong Python, chúng tôi sẽ nhận được lỗi cú pháp.
add[1, 4] = total
Cú pháp: Không thể gán cho chức năng gọi ở đây. Có thể bạn có nghĩa là '==' thay vì '='?
Trong Python 3.10, chúng tôi nhận được một số thông tin thêm về lỗi cho thấy rằng chúng tôi có thể muốn thực hiện thử nghiệm so sánh bằng toán tử
GFG16 thay vì gán
GFG8.
Trong tuyên bố này
invest[initial_amount,top_company[5,year,year+1]] = subsequent_amount
Chúng ta có thể kết luận hai điều:
1. Sử dụng bất hợp pháp toán tử chuyển nhượng. Đây là lỗi cú pháp khi chúng tôi gán giá trị hoặc trả về giá trị theo hàm cho một biến. Biến phải ở phía bên trái của toán tử gán và gọi giá trị hoặc chức năng ở phía bên phải. This is a syntax error when we assign a value or a value return by a function to a variable. The variable should be on the left side of the assignment operator and the value or function call on the right side.
Thí dụ
subsequent_amount = invest[initial_amount,top_company[5,year,year+1]]
2. Quên đặt Double == Toán tử để so sánh.
Đây là một lỗi ngữ nghĩa khi chúng tôi đặt toán tử gán [=] thay vì so sánh [==].
Thí dụ
invest[initial_amount,top_company[5,year,year+1]] == subsequent_amount