Based on this thread , I have the following
import functools
def predicate(author, version, **others):
    def _predicate(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            func.meta = {
                'author':  author,
                'version': version
            }
            func.meta.update(others)
            func(*args, **kwargs)
        return wrapper
    return _predicate
but I am unable to get the simple use case of
@predicate('some author', 'some version')
def second_of_two(a, b):
    return b
to work as expected:
>>> second_of_two.meta['author']
'some author'
>>> second_of_two(1, 2)
2
What am I doing wrong here?
well, I don't see why you're using the functools stuff over here, whereas you need to make it a simple decorator with arguments:
def predicate(author, version, **others):
    def _predicate(func):
        func.meta = {
            'author':  author,
            'version': version
        }
        func.meta.update(others)
        return func
    return _predicate
@predicate('foo', 'bar')
def myfunc(i,j):
    return i+j
print myfunc.meta
print myfunc(1,2)
gives:
{'version': 'bar', 'author': 'foo'}
3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With