Python Forum
i got a decorator question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i got a decorator question
#1
i advertised it here on stackoverflow, until now there are not answers, i would be happy to any help were to search.

(someone has give me there a link in a comment, but i didn't understand the connection to my question).
Reply
#2
Generally speaking, when posting for help on a forum, you should do as much work as possible to make things easier on the people who would help you for free. In this case, you should have posted the question here, instead of expecting us to go somewhere else. I followed your link anyway, planning to copy-paste it here, but I don't understand your question. Are you asking about decorators? Or glob? Or something else?

You should:
Simplify your question. Glob not part of your question? Replace that part of the code with something simpler. Consider this for all imports - if the question can be asked without imports, it should be.
Be extremely clear on behavior - e.g. "here's some code [...] here's the behavior I want [...] here's the behavior I see..."
Provide enough code that we can run it to reproduce your issue, but provide enough details that we shouldn't have to necessarily actually run the code ourselves.

Also, it's good that you told us you posted your question to another site, but I don't see you mentioning that on SO. You should post a link back to this question as well, so that if you get an answer here, someone doesn't spend time duplicating effort there.
Reply
#3
I guess his question is how to make the decorator usable with and without parameters.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
I made an example. The wraps function just copies the docstring and some other stuff to the decorated function.


from functools import wraps


def decorator(*decorator_args, color="green", size=12, **decorator_kwargs):
    def wrapper(func):
        @wraps(func)
        def inner(*args, **kwargs):
            print("DEBUG OUTPUT")
            print(
                f"Deco args   : {decorator_args}",
                f"Deco kwargs : {decorator_kwargs}",
                f"Func        : {func}",
                f"args        : {args}",
                f"kwargs      : {kwargs}",
                sep="\n",
            )
            print("====================")
            return func(*args, **kwargs)
        return inner
    if len(decorator_args) == 1 and callable(decorator_args[0]):
        return wrapper(decorator_args[0])
    else:
        return wrapper




deco_print1 = decorator(print)
deco_print2 = decorator(1,2,3,4,5, color="red", size=42, foo=1337)(print)


@decorator
def deco_print3(*args, **kwargs):
    print(*args, **kwargs)


@decorator(1,2,3,4,5, color="red", size=42, foo=1337)
def deco_print4(*args, **kwargs):
    print(*args, **kwargs)


deco_print1("deco_print1")
deco_print3("deco_print3")
deco_print2("deco_print2")
deco_print4("deco_print4")
Output:
DEBUG OUTPUT Deco args : (<built-in function print>,) Deco kwargs : {} Func : <built-in function print> args : ('deco_print1',) kwargs : {} ==================== deco_print1 DEBUG OUTPUT Deco args : (<function deco_print3 at 0x7f7661c04ee0>,) Deco kwargs : {} Func : <function deco_print3 at 0x7f7661c04ee0> args : ('deco_print3',) kwargs : {} ==================== deco_print3 DEBUG OUTPUT Deco args : (1, 2, 3, 4, 5) Deco kwargs : {'foo': 1337} Func : <built-in function print> args : ('deco_print2',) kwargs : {} ==================== deco_print2 DEBUG OUTPUT Deco args : (1, 2, 3, 4, 5) Deco kwargs : {'foo': 1337} Func : <function deco_print4 at 0x7f7661c04280> args : ('deco_print4',) kwargs : {} ==================== deco_print4
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function-decorator , which is checking an access according to USERNAME Liki 6 615 Feb-17-2024, 03:36 AM
Last Post: deanhystad
  Decorator for a function with argument(s) banidjamali 1 1,857 Feb-09-2021, 11:55 AM
Last Post: Gribouillis
  Decorator is using in class,but not working mbilalshafiq 2 2,148 Jul-04-2020, 08:53 PM
Last Post: mbilalshafiq
  Decorator and namespace. JayIvhen 2 2,811 Oct-26-2018, 03:56 PM
Last Post: nilamo
  python decorator alfredocabrera 0 3,156 Feb-22-2017, 07:04 AM
Last Post: alfredocabrera

Forum Jump:

User Panel Messages

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