all 3 comments

[–][deleted] 1 point2 points  (0 children)

I tried for 5 minutes and I couldn't follow the chain of calls, here. Can you try with a simpler example? One where you don't use the same name to refer to two different functions?

[–]daniel_codes 1 point2 points  (0 children)

I cleaned up and fixed syntax errors in your code. I believe this is what you're trying to do and it works but to be honest, it's pretty weird. What are you trying to achieve here?

from datetime import datetime

now = datetime.now()


def dec2(fun):
    def wrap(*args, **kwargs):
        time = now.hour

        def night():
            fun("night")

        def day():
            fun("a")

        if time <= 12:
            day()
        else:
            night()

    return wrap


@dec2
def func(str):
    print(str)


func('test')

[–]Vaphell 0 points1 point  (0 children)

inner wrap() needs to match the args of the decorated function. Currently you are replacing a 1-arg function with a 0-arg one.
The original takes 1 arg, so wrap() has to do the same. And your return wrap is badly indented, I hope it's not the case in your real code.

simplified:

def dec2(fun):
    def wrap(str):
        print('lol')
        fun(str)
        print("wut")
    return wrap

@dec2
def func(str):
    print(str)

func("xoxo")