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[]
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+'[]']
1def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
2def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
3def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
4def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
5def 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+'[]']
7Output:
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+'[]']
8def 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[]
0test = {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[]
2def test[]:
print 'test'
def test2[]:
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i[]
3def 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+'[]']
8def 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[]
7def 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+'[]']
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
0def 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+'[]']
0def 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
7def 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
0def 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[]
2def 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[]
4def func[]: { .. } var=func var[] var[]8
def func[]: { .. } var=func var[] var[]9
GFG0
GFG1
def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
5def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
5GFG6
def 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+'[]']
1GFG9
def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
3def test[]:
print 'test'
def test2[]:
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i[]
4123 98 123 98 1232
GFG5
GFG6
def 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+'[]']
1123 98 123 98 1237
def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
3def 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 number1
odd number even number odd number2
odd number even number odd number3
def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
3odd number even number odd number2
odd number even number odd number6
def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
3odd number even number odd number2
odd number even number odd number9
def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
3Output:
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 40002
def test[]:
print 'test'
def test2[]:
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i[]
4240 400 40004
def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
5 240 400 40006
def test[]:
print 'test'
def test2[]:
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i[]
4240 400 40008
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+'[]']
6test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
01test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
02def test[]:
print 'test'
def test2[]:
print 'test2'
assign_list=[test,test2]
for i in assign_list:
i[]
4test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
04 test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
05def func[]: { .. } var=func var[] var[]0____25
test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
08def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
0test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
10test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
11test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
12def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
0test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
10odd number even number odd number6
test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
12def test[]:
print 'test'
def test2[]:
print 'test2'
mydict = {'test':'blabla','test2':'blabla2'}
for key, val in mydict.items[]:
eval[key+'[]']
0test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
10test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
19test = {test:'blabla', test2:'blabla2'}
for key, val in test.items[]:
key[]
12Output:
240 400 4000