0

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

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 (with ','.join(str(each) for each in args)) 的内容以查看它包含的内容,然后打印出来

<....my_Class 对象在 ...>

装饰器适用于只有一个 arg (self) 的方法。我在这里错过了什么吗?

4

1 回答 1

0

当您回答评论时,请注意它有效:

>>> 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
于 2013-11-01T22:12:57.850 回答