2

This is meant to calculate execution time and log / print it:

def timer(logger=None): def decorator(func): def wrapper(*args, **kwargs): start_time = perf_counter() result = func(*args, **kwargs) total_time = perf_counter() - start_time message = f'{func.__name__} Time: ' f'{total_time} seconds' if logger is not None: logger.info(message) else: print(message) return result return wrapper return decorator @timer def decorated(): print("Running ...") if __name__ == '__main__': decorated() 

I'm expecting this to print time, instead it complains about a missing argument:

TypeError: decorator() missing 1 required positional argument: 'func' 

However, when I do:

@timer(None) def decorated(): print("Running ...") 

or:

@timer(logger=None) def decorated(): print("Running ...") 

It works! what is this nonsense?

Running ... decorated Time: 2.3044999999999316e-05 seconds 

Notes:

  • To those marking my question as a duplicate, can you explain why other decorators work just fine?
  • In the example below, lru_cache accepts maxsize and typed parameters and works without explicit calls as well.

Example:

from functools import lru_cache @lru_cache def decorated(): print("Running ...") 

Out:

Running ... 
8
  • 5
    Does this answer your question? python decorator TypeError missing 1 required positional argument Commented Jan 8, 2021 at 9:55
  • 1
    Bare @timer returns decorator function. On the other hand, @timer() will execute timer first then return decorator which in turn used to decorate decorated. Commented Jan 8, 2021 at 9:58
  • @EricLeung It doesn't explain what is happening, and take for example functools.lru_cache, it works as well as many other decorators without explicit calls () Commented Jan 8, 2021 at 10:00
  • @bullseye it explains the reason if you read carefully. lru_cache is probably not a parametrizable decorator (however, I did not check), but your timer decorator is parametrizable, this is the key point. Commented Jan 8, 2021 at 10:04
  • 1
    @Tryph stackoverflow.com/q/653368/476 Commented Jan 8, 2021 at 10:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.