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

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

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()
3
test = {test:'blabla', test2:'blabla2'}

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

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

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

for key, val in test.items():
    key()
7

test = {test:'blabla', test2:'blabla2'}

for key, val in test.items():
    key()
8
test = {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+'()')
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+'()')
2
test = {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+'()')
6
def 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+'()')
8
def test():
        print 'test'

def test2():
        print 'test2'

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

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

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

for key, val in test.items():
    key()
5
def test():
    print 'test'

def test2():
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i()
8
def test():
    print 'test'

def test2():
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i()
9
def func():
{
..
}

var=func

var()
var()
0
def func():
{
..
}

var=func

var()
var()
1

test = {test:'blabla', test2:'blabla2'}

for key, val in test.items():
    key()
4
def test():
    print 'test'

def test2():
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i()
4

def func():
{
..
}

var=func

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

def test():
        print 'test'

def test2():
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
        eval(key+'()')
8
GFG
2
GFG
3
GFG
4
GFG
5
test = {test:'blabla', test2:'blabla2'}

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

for key, val in test.items():
    key()
9

123
98
123
98
123
0
test = {test:'blabla', test2:'blabla2'}

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

for key, val in test.items():
    key()
5
123
98
123
98
123
3
test = {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+'()')
8
123
98
123
98
123
6
GFG
9

123
98
123
98
123
0
test = {test:'blabla', test2:'blabla2'}

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

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

for key, val in test.items():
    key()
7

def func():
{
..
}

var=func

var()
var()
4
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items():
    key()
9
odd number
even number
odd number
5

odd number
even number
odd number
6
odd number
even number
odd number
7
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items():
    key()
7

odd number
even number
odd number
6
240
400
4000
0
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items():
    key()
7

odd number
even number
odd number
6
240
400
4000
3
test = {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
4000
6

def test():
        print 'test'

def test2():
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items():
        eval(key+'()')
8
240
400
4000
8
test = {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()2
test = {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()
4
test = {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()
4
test = {test:'blabla', test2:'blabla2'}

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

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

for key, val in test.items():
    key()
06

test = {test:'blabla', test2:'blabla2'}

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

for key, val in test.items():
    key()
04
240
400
4000
0
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items():
    key()
06

test = {test:'blabla', test2:'blabla2'}

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

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

for key, val in test.items():
    key()
13
test = {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.