Python Forum
decorators within decorators
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
decorators within decorators
#5
Just wrap the the first decorator into a wrapper function and return it.

def superdecorator(decorator):
    def wrapper(*args, **kwargs):
        def decorator(func):
            def wrapped(*args, **kwargs):
                print('{} * {} = '.format(*args*n,**kwargs*n), end='')
                return func(*args*n, **kwargs*n)
            return wrapped
        return decorator
    return wrapper
But you will have a problem with the function names.
Actually this happens with the first decorator too. If you have any docstrings in the decorated function you will lose it. See functools.wraps
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
decorators within decorators - by mp3909 - Mar-26-2018, 04:57 AM
RE: decorators within decorators - by hf8bm - Mar-26-2018, 07:47 AM
RE: decorators within decorators - by wavic - Mar-26-2018, 08:06 AM
RE: decorators within decorators - by mp3909 - Mar-28-2018, 03:53 PM
RE: decorators within decorators - by wavic - Mar-28-2018, 05:28 PM
RE: decorators within decorators - by snippsat - Mar-28-2018, 07:47 PM
RE: decorators within decorators - by hf8bm - Apr-02-2018, 09:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Decorators @ annotation drcl 3 557 Feb-24-2024, 06:12 AM
Last Post: Gribouillis
  Does @ at sign used for tother than decorators? ggpython000 1 615 Oct-12-2023, 09:08 AM
Last Post: buran
  Variable Scopes Decorators oclmedyb 6 2,841 Jan-15-2021, 02:13 PM
Last Post: oclmedyb
  Decorators and return statements JonEdward 2 1,983 Jul-24-2020, 05:02 PM
Last Post: JonEdward
  How to print cache from Decorators with Memoization OlgaM 2 2,147 Jan-29-2020, 05:06 PM
Last Post: OlgaM
  Python decorators. dodiko1d 0 10,442 Oct-13-2019, 10:23 AM
Last Post: dodiko1d
  Decorators yksingh1097 2 2,605 Aug-14-2018, 01:44 PM
Last Post: yksingh1097
  learning decorators Prince_Bhatia 6 3,422 Aug-13-2018, 02:28 PM
Last Post: Prince_Bhatia
  decorators mp3909 9 5,847 Mar-25-2018, 05:28 PM
Last Post: mp3909

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020