Python中关于decorator的bug

2024-04-19 18:38:23 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下:

import time
import sys

def LogClassFuncInfos(func):
    def wrapper(*s, **gs):
        methodName = format(func.__name__)
        print(methodName)
        func(*s, **gs)
    return wrapper

class NameModel():

    def __init__(self):
        self.name = 'test'

    @LogClassFuncInfos
    def getName(self):
        return self.name

a = NameModel()
print(a.getName())

代码应该打印'test',但是代码打印None。你知道吗

如果移除def getName(self)之前的@LogClassFuncInfos,则'test'可以正常打印。你知道吗


Tags: 代码nametestimportselfgsreturndef