方法的参数不被传递给装饰器

嘿我有一个方法定义如下:

from x.y import util
class my_Class(some_object):
    @util.myDecorator
    def foo(self, log_file):
    '''
    Test that search entered into the search bar is the search being
    executed in the job.
    '''
    self.some_page.open()
    textarea = self.some_page.searchbar

    searchbar.run_search(log_file.search_string)
    self.browser.capture_screenshot()
    self.some_page.jobstatus.wait_for_job_complete()

    self.verify_equals(
        self.some_page.jobstatus.event_count,
        log_file.event_count,
        "Event count doesn't seem to be right.")

装饰器在util.py文件中

def mydecorator(func):
    def timeit(*args, **kwargs):
        start_time = time.time()
        ret=func(*args, **kwargs)
        end_time = time.time()
        print end_time - start_time
        return ret
    return timeit

当我尝试执行代码时,它在ret=func(*args, **kwargs)失败并显示错误消息

TypeError:foo()只需要2个参数(给出1)

  • 我打印了* args的内容(用','.join(str(each) for each in args) )来查看它包含的内容,并将其打印出来
  • <.... my_Class对象在...>中

    装饰器对于只有一个参数(self)的方法工作正常。 我在这里错过了什么吗?


    如果你回答评论,请注意它的作用:

    >>> import time
    >>>
    >>> def mydecorator(func):
    ...     def timeit(*args, **kwargs):
    ...         start_time = time.time()
    ...         ret=func(*args, **kwargs)
    ...         end_time = time.time()
    ...         print end_time - start_time
    ...         return ret
    ...     return timeit
    ...
    >>> class my_Class(object):
    ...     @mydecorator
    ...     def foo(self, arg_1):
    ...         print arg_1
    ...
    >>> my_Class().foo(100)
    100
    0.0
    
    链接地址: http://www.djcxy.com/p/63941.html

    上一篇: method's arguments not being passed to decorator

    下一篇: How to download Google Map for Offline Mode in Android