> Would it make sense to have both options?

If you have the wrapper pattern, you already have both options. This would
be my preferred way of doing it but harder to implement, particularly in
any way which significantly mimics Python's way of doing it.

> One problem with the wrapper pattern is that if you have multiple
decorators then wouldn't that end up calling the function multiple times?
Not so with the before/after.  It really depends on the intended behavior.
How does Python and others handle this?

Python the @decorator syntax is just sugar for re-assigning a method so
they are easily stacked. Say we have a trivial decorator:

def timer(func):
    def wrap():
        start = time.perf_counter()
        func(*args)
        end = time.perf_counter()
        print("Did thing in this many seconds...")
    return wrap

Then

@timer
def foo(a, b, c):
    # do some stuff

is equivalent to foo = timer(foo)
and

@timer
@somethingElse
def foo(a, b, c):
    # do some stuff

is equivalent to foo = timer(somethingElse(foo))

Regards,
David

On Mon, Mar 15, 2021 at 1:46 AM Peter Stalman <sarke...@gmail.com> wrote:

> On Sun, Mar 14, 2021 at 6:34 PM Peter Stalman <sarke...@gmail.com> wrote:
>
>> Would it make sense to have both options?
>>
>
> 6) Multiple decorators on the same function?
> One problem with the wrapper pattern is that if you have multiple
> decorators then wouldn't that end up calling the function multiple times?
> Not so with the before/after.  It really depends on the intended behavior.
> How does Python and others handle this?
>
> Thanks,
> Peter
>
>
>

Reply via email to