简体   繁体   English

Decorator和error function()参数1必须是代码,而不是str

[英]Decorator and error function() argument 1 must be code, not str

I have next code 我有下一个代码

def timer_dec(f):
    def wrapper(*args, **kwargs):
        t = time.time()
        args[0].debug('<{}> start'.format(f.__name__))
        res = f(*args, **kwargs)
        args[0].debug('<{}> finish'.format(f.__name__))
        args[0].debug("Working time for function <%s>: %f" % (f.__name__, time.time() - t))
        return  res

    return wrapper

This is works fine: 这很好用:

@timer_dec
class A(object):
  pass

But this is not working: 但这不起作用:

@timer_dec
class A(object):
  pass

class B(A):
  pass

TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str TypeError:调用元类base函数时出错()参数1必须是代码,而不是str

Python version is 2.7 Python版本是2.7

You seem to be using a function decorator as a class decorator. 您似乎使用函数装饰器作为类装饰器。

@timer_dec
class A(object):
  pass

is equivalent to 相当于

class A(object):
  pass
A = timer_dec(A)

Because timer_dec returns a function, A is now a function. 因为timer_dec返回一个函数,所以A现在是一个函数。


You can create a class decorator that applies a function decorator to all methods of the class. 您可以创建一个类装饰器,将函数装饰器应用于该类的所有方法。 See here for an example: Alex Martelli's answer to Applying python decorators to methods in a class 请参阅此处的示例: Alex Martelli将Python python装饰器应用于类中的方法的答案

Your decorator is returning a function. 你的装饰者正在返回一个函数。 Thus, after the decorator call, the name "A" is bound to a function, not a class. 因此,在装饰器调用之后,名称“A”绑定到函数,而不是类。 Then, you try to inherit B from the function A, which is illegal. 然后,您尝试从函数A继承B,这是非法的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用元类基时出错:function() 参数 1 必须是代码,而不是 str - Error when calling the metaclass bases: function() argument 1 must be code, not str 调用元类库函数时出错()参数1必须是代码而不是str - error when calling the metaclass bases function() argument 1 must be code not str 类型错误:函数()参数“代码”必须是代码,而不是 str - TypeError: function() argument 'code' must be code, not str 类型错误:function() 参数 1 必须是代码,而不是 str - TypeError: function() argument 1 must be code, not str TypeError:函数()参数“代码”必须是代码,而不是 Azure Synapse Notebook 中的 str - TypeError: function() argument 'code' must be code, not str in Azure Synapse Notebook 我试图从我的 pandas DataFrame 创建一个 .CSV 文件,但它给了我一个错误“函数()参数 1 必须是代码,而不是字符串” - I was trying to create a .CSV file from my pandas DataFrame but it gives me an error " function() argument 1 must be code, not str" 替换不接受列表作为参数的函数-熊猫。 收到错误TypeError:replace()参数1必须是str,而不是list - Replace function not accepting list as argument - pandas. Getting error TypeError: replace() argument 1 must be str, not list Python错误:参数1必须为缓冲区或字节,不能为str - Python Error: Argument 1 must be buffer or bytes not str join() 参数必须是 str 或 bytes,而不是 'DataFrame' 错误 - join() argument must be str or bytes, not 'DataFrame' error 线程异常写入函数中的线程10错误,TypeError:write()参数必须为str,而不是字节 - Exception in thread Thread-10 error in write function, TypeError: write() argument must be str, not bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM