Hướng dẫn how do you call a variable name in python with a function? - làm thế nào để bạn gọi một tên biến trong python bằng một hàm?

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

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

for key, val in test.items[]:
    key[]
0:

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[]
1.

Đã 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

Trong Python, các hàm là các đối tượng "hạng nhất '': chúng có thể có các tên biến được gán cho chúng, chúng có thể được truyền dưới dạng đối số cho các chức năng khác và thậm chí có thể được trả lại từ các chức năng khác. , nhưng tên đó có thể được chỉ định lại để giới thiệu một đối tượng khác nếu muốn [đừng làm điều này trừ khi bạn muốn nói!]

Như ví dụ sau đây chứng minh, có thể nhiều hơn một tên biến được gán cho cùng một đối tượng hàm.

>>> def cosec[x]:
...    """Return the cosecant of x, cosec[x] = 1/sin[x]."""
...    return 1./math.sin[x]
...
>>> cosec

>>> cosec[math.pi/4]
1.4142135623730951
>>> csc = cosec
>>> csc

>>> csc[math.pi/4]
1.4142135623730951

Bài tập

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

for key, val in test.items[]:
    key[]
2 liên kết trình định danh [tên biến]
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
3 với cùng một đối tượng hàm với định danh
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
4: Hàm này sau đó có thể được gọi với
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
5 cũng như với
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
6.

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[]
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
def test[]:
        print 'test'

def test2[]:
        print 'test2'

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

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
3

def test[]:
        print 'test'

def test2[]:
        print 'test2'

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

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
5
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

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+'[]']
8
def test[]:
        print 'test'

def test2[]:
        print 'test2'

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

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
0

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

for key, val in test.items[]:
    key[]
7
def test[]:
    print 'test'

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
2
def test[]:
    print 'test'

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
3

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
def test[]:
        print 'test'

def test2[]:
        print 'test2'

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

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
7

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+'[]']
0
>>> def cosec[x]:
...    """Return the cosecant of x, cosec[x] = 1/sin[x]."""
...    return 1./math.sin[x]
...
>>> cosec

>>> cosec[math.pi/4]
1.4142135623730951
>>> csc = cosec
>>> csc

>>> csc[math.pi/4]
1.4142135623730951
0

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+'[]']
0
def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
1
>>> def cosec[x]:
...    """Return the cosecant of x, cosec[x] = 1/sin[x]."""
...    return 1./math.sin[x]
...
>>> cosec

>>> cosec[math.pi/4]
1.4142135623730951
>>> csc = cosec
>>> csc

>>> csc[math.pi/4]
1.4142135623730951
4
>>> def cosec[x]:
...    """Return the cosecant of x, cosec[x] = 1/sin[x]."""
...    return 1./math.sin[x]
...
>>> cosec

>>> cosec[math.pi/4]
1.4142135623730951
>>> csc = cosec
>>> csc

>>> csc[math.pi/4]
1.4142135623730951
5
>>> def cosec[x]:
...    """Return the cosecant of x, cosec[x] = 1/sin[x]."""
...    return 1./math.sin[x]
...
>>> cosec

>>> cosec[math.pi/4]
1.4142135623730951
>>> csc = cosec
>>> csc

>>> csc[math.pi/4]
1.4142135623730951
6
>>> def cosec[x]:
...    """Return the cosecant of x, cosec[x] = 1/sin[x]."""
...    return 1./math.sin[x]
...
>>> cosec

>>> cosec[math.pi/4]
1.4142135623730951
>>> csc = cosec
>>> csc

>>> csc[math.pi/4]
1.4142135623730951
7

def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
0
>>> def cosec[x]:
...    """Return the cosecant of x, cosec[x] = 1/sin[x]."""
...    return 1./math.sin[x]
...
>>> cosec

>>> cosec[math.pi/4]
1.4142135623730951
>>> csc = cosec
>>> csc

>>> csc[math.pi/4]
1.4142135623730951
0

def func[]:
{
..
}

var=func

var[]
var[]
0
def test[]:
        print 'test'

def test2[]:
        print 'test2'

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

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
2

def func[]:
{
..
}

var=func

var[]
var[]
3

def func[]:
{
..
}

var=func

var[]
var[]
3

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[]
7
def func[]:
{
..
}

var=func

var[]
var[]
6

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[]
8
def func[]:
{
..
}

var=func

var[]
var[]
9
GFG
0
GFG
1
def test[]:
        print 'test'

def test2[]:
        print 'test2'

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

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
5

GFG
6
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
GFG
9
def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
3

def test[]:
    print 'test'

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
4
123
98
123
98
123
2
GFG
5

GFG
6
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
123
98
123
98
123
7
def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
3

def func[]:
{
..
}

var=func

var[]
var[]
0
def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
5
odd number
even number
odd number
1

odd number
even number
odd number
2
odd number
even number
odd number
3
def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
3

odd number
even number
odd number
2
odd number
even number
odd number
6
def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
3

odd number
even number
odd number
2
odd number
even number
odd number
9
def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
3

Output:

odd number
even number
odd number

Ví dụ 3:

Python3

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

for key, val in test.items[]:
    key[]
7
240
400
4000
2

def test[]:
    print 'test'

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
4
240
400
4000
4
def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
5
240
400
4000
6

def test[]:
    print 'test'

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
4
240
400
4000
8
def test[]:
        print 'test'

def test2[]:
        print 'test2'

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

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
6
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
01
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
02

def test[]:
    print 'test'

def test2[]:
    print 'test2'

assign_list=[test,test2]

for i in assign_list:
    i[]
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

def func[]:
{
..
}

var=func

var[]
var[]
0____25
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
08

def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
0
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
10
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
11
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
12

def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
0
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
10
odd number
even number
odd number
6
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
12

def test[]:
        print 'test'

def test2[]:
        print 'test2'

mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
        eval[key+'[]']
0
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
10
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
19
test = {test:'blabla', test2:'blabla2'}

for key, val in test.items[]:
    key[]
12

Output:

240
400
4000

Bài Viết Liên Quan

Chủ Đề