Hướng dẫn how to call a function from a variable in python - cách gọi một hàm từ một biến trong python

def test(): print 'test' def test2(): print 'test2' test = {'test':'blabla','test2':'blabla2'} for key, val in test.items(): key() # Here i want to call the function with the key name, how can i do so?

Hỏi ngày 13 tháng 12 năm 2010 lúc 16:50Dec 13, 2010 at 16:50

1

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.

test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key()

Đã trả lời ngày 13 tháng 12 năm 2010 lúc 16:53Dec 13, 2010 at 16:53

John Kugelmanjohn KugelmanJohn Kugelman

339K67 Huy hiệu vàng514 Huy hiệu bạc563 Huy hiệu Đồng67 gold badges514 silver badges563 bronze badges

0

John có một giải pháp tốt. Đây là một cách khác, sử dụng eval():

def test(): print 'test' def test2(): print 'test2' mydict = {'test':'blabla','test2':'blabla2'} for key, val in mydict.items(): eval(key+'()')

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 test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 0.

Đã trả lời ngày 13 tháng 12 năm 2010 lúc 17:00Dec 13, 2010 at 17:00

ChrisayCockchrisaycockchrisaycock

35K14 Huy hiệu vàng88 Huy hiệu bạc122 Huy hiệu đồng14 gold badges88 silver badges122 bronze badges

4

def test(): print 'test' def test2(): print 'test2' assign_list=[test,test2] for i in assign_list: i()

2

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() 2

test = {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() 7

test = {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+'()') 0

def test(): print 'test' def test2(): print 'test2' mydict = {'test':'blabla','test2':'blabla2'} for key, val in mydict.items(): eval(key+'()') 1

Output:  

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+'()') 4

test = {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+'()') 7

def 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() 1

def 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() 4

def 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()1

test = {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() 4

def 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+'()') 6

def func(): { .. } var=func var() var()7

def func(): { .. } var=func var() var()7

Output:

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 GFG0

def test(): print 'test' def test2(): print 'test2' mydict = {'test':'blabla','test2':'blabla2'} for key, val in mydict.items(): eval(key+'()') 8GFG2 GFG3GFG4 GFG5 test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 9test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 9

123 98 123 98 1230test = {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 1233test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 7

def test(): print 'test' def test2(): print 'test2' mydict = {'test':'blabla','test2':'blabla2'} for key, val in mydict.items(): eval(key+'()') 8123 98 123 98 1236GFG9

123 98 123 98 1230test = {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 number1test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 7

def func(): { .. } var=func var() var()4test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 9 odd number even number odd number5

odd number even number odd number6odd number even number odd number7test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 7

odd number even number odd number6240 400 40000test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 7

odd number even number odd number6240 400 40003test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 7

Output:

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 40006

def test(): print 'test' def test2(): print 'test2' mydict = {'test':'blabla','test2':'blabla2'} for key, val in mydict.items(): eval(key+'()') 8240 400 40008test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 9 eval()0

def 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()6

def test(): print 'test' def test2(): print 'test2' mydict = {'test':'blabla','test2':'blabla2'} for key, val in mydict.items(): eval(key+'()') 8eval()8 eval()9

def 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() 02

test = {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() 06

test = {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 40000test = {test:'blabla', test2:'blabla2'} for key, val in test.items(): key() 06

test = {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() 06

Output:

240 400 4000

Bạn có thể gọi một chức năng trong một biến không?

Có hai phương thức để gọi một hàm từ chuỗi được lưu trữ trong một biến.Chế độ đầu tiên là bằng cách sử dụng phương thức đối tượng cửa sổ và phương thức thứ hai là bằng cách sử dụng phương thức eval ().. The first one is by using the window object method and the second one is by using eval() method.

Bạn có thể gán một cuộc gọi chức năng cho một biến trong Python không?

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.

Chủ đề