Python将函数名称作为变量调用

时间:2017-05-11 21:31:20

标签: python python-3.x formatting

我有一个基本的排序和搜索循环。最后,我要感谢用户并显示所用函数的名称。是否有format要求这样做?

def arr_sort_binsearch(ar):

    item = 8

    ar.sort()
    l=0
    r=len(ar)-1

    while l<r:
        if (ar[l] + ar[r] == item):
            print("{} and {} is: {}".format(ar[l],ar[r],item))
            l += 1
            r -= 1
        elif (ar[l] + ar[r] < item):
            l += 1
        else:
            r -+ 1
    print("Thanks for using {}".format(name_of_function?)

1 个答案:

答案 0 :(得分:0)

这里你去:

How to get a function name as a string in Python?

替换“name_of_function?”用:

arr_sort_binsearch.__name__