'Why TypeError: 'str' object is not callable error has occurred in my code
I am new to learning python, I know this kind questions asked before but i am not able to find any solutions for it. Please check my code and correct me about decorator's functionality, Thank you.
def uppercase(func_one):
func_one = func_one()
return func_one.upper()
def split(func_two):
func_two = func_two()
return func_two.split()
@split
@uppercase
def CallFunction():
return "my string was in lower case"
res = CallFunction()
print(res)
Solution 1:[1]
Decorators are confusing and probably should be avoided till you are super experienced with python. That being said, chaining decorators is even more tricky:
from functools import wraps
def split(fn): # fn is the passed in function
@wraps(fn) # This means we can grabs its args and kwargs
def wrapped(*args, **kwargs): # This is the new function declaration
return fn(*args, **kwargs).split()
return wrapped
def uppercase(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
return fn(*args, **kwargs).upper()
return wrapped
# Order matters. You can't call .upper() on a list
@split
@uppercase
def CallFunction():
return "my string was in lower case"
res = CallFunction()
print(res)
Alternatively if you don't want the order of these two decorators to matter than you need to handle the list
case:
def uppercase(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
result = fn(*args, **kwargs)
if isinstance(result, list):
return [x.upper() for x in result]
return result.upper()
return wrapped
Reference: How to make a chain of function decorators?
Solution 2:[2]
You don't even need functools
, you just need to grab the args you are passing.
Here's what you are missing: Add the args being passed inside a wrapper and define that wrapper to accept the args passed. Happy coding!
def uppercase(func_one):
def wrapper(*args):
x = func_one()
return x.upper()
return wrapper
def split(func_two):
def wrapper(*args):
y = func_two()
return y.split()
return wrapper
@split
@uppercase
def CallFunction():
return "my string was in lower case"
res = CallFunction()
print(res)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Vijeth Kashyap |