decorator icon indicating copy to clipboard operation
decorator copied to clipboard

[BUG] kwargs are not respected

Open mlguruz opened this issue 1 year ago • 3 comments

If I just ran the example from the doc:

from decorator import decorator

@decorator
def warn_slow(func, timelimit=60, *args, **kw):
    print(kw)
    t0 = time.time()
    result = func(*args, **kw)
    dt = time.time() - t0
    if dt > timelimit:
        logging.warn('%s took %d seconds', func.__name__, dt)
    else:
        logging.info('%s took %d seconds', func.__name__, dt)
    return result


@warn_slow  # warn if it takes more than 1 minute
def preprocess_input_files(inputdir, tempdir,):
    ...
    
    
preprocess_input_files(inputdir='a', tempdir='b')

print(kw) shows empty dict, which It should not right ?

mlguruz avatar May 10 '23 21:05 mlguruz

It is perfectly right. This is how argument passing works in Python, even without any decorator.

micheles avatar May 11 '23 03:05 micheles

I'm sorry - I must be missing something obvious here:

If I call func(*a, **k) as func(inputdir='a', tempdir='b), shouldn't a = (), and k = dict(inputdir='a', tempdir='b) ?

mlguruz avatar May 11 '23 03:05 mlguruz

Yes, but you are calling func(inputdir, tempdir, **k)

micheles avatar May 11 '23 03:05 micheles