Search code examples
pythondecorator

i tried write the first code in a different way. first is okay second also runs but i got a " nontype object is not callable error". why is that?


in this python code here i get error "nonetype object is callable". why is that? thanks

`#firstcode

def decor(func):
    def wrap():
        print("============")
        func()
        print("============")
    return wrap

def print_text():
    print("Hello world!")

print_text = decor(print_text)
print_text()

#secondcode

def wrap(func):
    print("============")
    func()
    print("============")


def print_text():
    print("Hello world!")

print_text = wrap(print_text)
print_text()`

i tried to write code in another way and i got error. actually maybe the problem is that wrap function does not return anything here and i call it to reassign print_text. but i couldn't understand it well. any suggestions?


Solution

  • In your second code, wrap does not return anything i.e. it returns None.

    Thus:

    print_text = wrap(print_text)
    

    Is equivalent to:

    print_text = None
    

    You are trying to call print_text like a function but it is None.