'defining an appropriate variable name
How can i catch this very basic error before it occurs ?
# create a variable
str = 10
print(str)
print(type(str))
# change from integer to string
str = str(str)
print(str)
The error is:
'int' object is not callable
I would assert that str is a keyword and a standard linter should pick this up, but my linter does not because str is in fact not a keyword (https://realpython.com/lessons/reserved-keywords/).
The same is true for int, list etc.
So how to catch or prevent this error before it occurs ?
Solution 1:[1]
The str you are using is a function in python. So when you are assigning str a value, you have overridden the function address stored in str with some value. So the str is no more a function. So when you tried to call str() it returned the error.
Look at the below output for better understanding.
>>> str(123)
'123'
>>> str = 4
>>> str(123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
This output is from python command line interpreter. Hope this helped.
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 | BALAJI VARA PRASAD |
