Bạn có thể sử dụng các đối tượng chức năng thực tế làm khóa, thay vì tên của các hàm. Các chức năng là các đối tượng hạng nhất trong Python, vì vậy nó sạch hơn và thanh lịch hơn để sử dụng chúng trực tiếp hơn là tên của chúng.
John có một giải pháp tốt. Đây là một cách khác, sử dụng eval()
:
Lưu ý rằng tôi đã thay đổi tên của từ điển để ngăn chặn một cuộc đụng độ với tên của hàm
Cải thiện bài viết
Lưu bài viết
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;
Thực hiện
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ề.
Syntax::
def func():
{
..
}
var=func
var()
var()
Example:
Python3
test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
1 test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
2test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
3test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
5test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
6test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
7test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
8test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
0def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
1Output:
GFG
Các chương trình sau đây sẽ giúp bạn hiểu rõ hơn:
Ví dụ 1: & nbsp;
Python3
def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
2test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9 def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
1 def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
6def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
7def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
8def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
22test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9 def test():
print 'test'
def test2():
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i()
1def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
8test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4def test():
print 'test'
def test2():
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i()
4def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
8test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
5def test():
print 'test'
def test2():
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i()
8def test():
print 'test'
def test2():
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i()
9def func():
{
..
}
var=func
var()
var()
0def func():
{
..
}
var=func
var()
var()
1test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4def test():
print 'test'
def test2():
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i()
4def func():
{
..
}
var=func
var()
var()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9 def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
6def func():
{
..
}
var=func
var()
var()
7def func():
{
..
}
var=func
var()
var()
7Output:
123
98
123
98
123
Ví dụ 2: Hàm được tham số hóaparameterized function
Python3
test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
1 GFG
0def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
8GFG
2 GFG
3GFG
4 GFG
5 test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9123
98
123
98
123
0test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
5123
98
123
98
123
3test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
7def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
8123
98
123
98
123
6GFG
9123
98
123
98
123
0test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
5odd number
even number
odd number
1test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
7def func():
{
..
}
var=func
var()
var()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9 odd number
even number
odd number
5odd number
even number
odd number
6odd number
even number
odd number
7test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
7odd number
even number
odd number
6240
400
4000
0test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
7odd number
even number
odd number
6240
400
4000
3test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
7Output:
odd number
even number
odd number
Ví dụ 3:
Python3
test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
1 240
400
4000
6def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
8240
400
4000
8test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9 eval()
0def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
8eval()
2test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9 def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
0eval()
5eval()
6def test():
print 'test'
def test2():
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
eval(key+'()')
8eval()
8 eval()
9def func():
{
..
}
var=func
var()
var()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
9 test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
02test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
04test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
05test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
06test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
04240
400
4000
0test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
06test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
04test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
13test = {test:'blabla', test2:'blabla2'}
for key, val in test.items():
key()
06Output:
240
400
4000
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.we can assign a function to a variable. And using that variable we can call the function as many as times we want.