Why can wrapper() call txt but italic() cannot (when using it as a decorator)?
Helpful if you can explain the order things get executed, and how the call to fun within the wrapper is different than calling in italic as a decorator (example 1,2) and not (example 3). Thanks!
# EXAMPLE 1: call function with wrapper
def italic(fun):
def wrapper():
return '<i>' + fun() + '</i>'
return wrapper
@italic
def txt():
return 'merp'
print(txt())
Output:
<i>merp</i>
# EXAMPLE 2: call function directly
def italic(fun):
return '<i>' + fun() + '</i>'
@italic
def txt():
return 'merp'
print(txt())
Output:
TypeError: 'str' object is not callable
Why does it see txt as a string here? But if I try return '<i>' + fun + '</i>'
, it says cannot concatenate function to string lol
edit: nvm, the type error and uncallable errors were from two different lines; i got them confused ><
# EXAMPLE 3: nest functions instead of using as decorator
def italic(fun):
return '<i>' + fun() + '</i>'
def txt():
return 'merp'
print(italic(txt))
Output:
<i>merp</i>