Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 此代码错误:不可调用非类型对象_Python_Decorator - Fatal编程技术网

Python 此代码错误:不可调用非类型对象

Python 此代码错误:不可调用非类型对象,python,decorator,Python,Decorator,我正在学习python中的装饰程序。 试图创建一个声明器类,如下所示,但出现错误“NoneType”对象不可调用。 任何人都可以帮忙。太棒了 def decorator_class(object): def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): print('call function run b4 {}'.format(self.f

我正在学习python中的装饰程序。 试图创建一个声明器类,如下所示,但出现错误“NoneType”对象不可调用。 任何人都可以帮忙。太棒了

def decorator_class(object):
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        print('call function run b4 {}'.format(self.func.__name__))
        return self.func(*args, **kwargs)


@decorator_class
def cities(a, b):
    print('cities function ran are {} and {}'.format(a, b))


cities('a', 'b')

您的类不是一个类,而是一个函数

改变

def decorator_class(object):

完整代码:

class decorator_class(object):
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        print('call function run b4 {}'.format(self.func.__name__))
        return self.func(*args, **kwargs)

@decorator_class
def cities(a, b):
    print('cities function ran are {} and {}'.format(a, b))

cities('a', 'b')
输出:

call function run b4 cities
cities function ran are a and b

粘贴完整的错误codeclass decorator_class:,而不是def…我建议您使用IDE来开发代码。这类错误在这些工具中会变得非常明显,从而为您省去了麻烦
call function run b4 cities
cities function ran are a and b