小编Par*_*rar的帖子

如何通过键入函数名称而不是使用括号来调用python中的函数

首先要找到两个数字的"lcm"我做了一个函数lcm(a, b).然后我想找到"hcf",所以我做了一个装饰器decorhcf(a, b)在其中定义了一个函数.然后我通过输入函数的名称返回此函数,我没有使用括号,但它仍然有效.我不明白为什么这个功能工作,即使我没有使用括号.

def decor(lcm_arg):        # just to practice decorators
    def hcf(a, b):
        if a > b:
            a, b = b, a
        while True:
            if b % a == 0:
                print("hcf is", a)
                break
            else:
                a, b = b % a, a
        return lcm_arg(a, b)
    return hcf              # how hcf function is working without using brackets

@decor
def lcm(a, b):
    if a > b:
        a, b = b, a
    for x in range(b, a*b+1, …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-decorators

1
推荐指数
1
解决办法
110
查看次数

标签 统计

python ×1

python-3.x ×1

python-decorators ×1