method's arguments not being passed to decorator

hey I have a method defined as follows:

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.")

the decorator is in the file 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

when i try executing the code, it fails at ret=func(*args, **kwargs) with the error message

TypeError: foo() takes exactly 2 arguments (1 given)

  • I printed the contents of *args (with ','.join(str(each) for each in args) ) to see what it contained, and it printed out
  • <....my_Class object at ...>

    the decorator works fine for methods with just one arg (self) . Am i missing something here?


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

    >>> 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/63942.html

    上一篇: std :: atomic的锁在哪里?

    下一篇: 方法的参数不被传递给装饰器