Discuss / Python / 又一个问题,愁。

又一个问题,愁。

Topic source

renjie0310

#1 Created at ... [Delete] [Delete and Lock User]

def logit(func):

       @wraps(func)

       def with_logging(*args, **kwargs):

                    print(func.__name__ + " was called")

                     return func(*args, **kwargs)

        return with_logging

@logit

def addition_func(x):

   """Do some math."""

   return x + x

pp=addition_func(4)

运行结果:

addition_func was called

再次敲入

>>> pp

运行结果

8

这是啥原理?彻底蒙圈了,感觉函数这块简直不要太难了。

金葛城

#2 Created at ... [Delete] [Delete and Lock User]

这里有什么问题吗?

renjie0310

#3 Created at ... [Delete] [Delete and Lock User]

我觉得执行pp=addition_func(4)这一句时应该8也应该显示出来。

renjie0310

#4 Created at ... [Delete] [Delete and Lock User]

好像理解了,pp=addition_func(4),相当于logit(addition_func)(4)直接执行with_logging函数, return func(*args, **kwargs)运行结果赋值给了pp,执行PP返回执行结果。


  • 1

Reply