![]() |
i got a decorator question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: i got a decorator question (/thread-29197.html) |
i got a decorator question - yosef - Aug-22-2020 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). RE: i got a decorator question - micseydel - Aug-24-2020 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. RE: i got a decorator question - DeaD_EyE - Aug-25-2020 I guess his question is how to make the decorator usable with and without parameters.
RE: i got a decorator question - DeaD_EyE - Aug-25-2020 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")
|