Weirdness calling str() to convert integer to string in Python 3? [duplicate] - python

This question already has answers here:
Why does code like `str = str(...)` cause a TypeError, but only the second time?
(20 answers)
Closed last month.
Why is this giving me an error?
>>> variable = str(21)
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
variable = str(21)
TypeError: 'str' object is not callable

That code alone won't give you an error. For example, I just tried this:
~ $ python3.2
>>> variable = str(21)
>>> variable
'21'
Somewhere in your code you're defining that str = something else, masking the builtin definition of str. Remove that and your code will work fine.

Because you've probably overwritten the str function by calling your own variable str.

Related

Python – 'type' error in the function header [duplicate]

This question already has answers here:
"TypeError: 'type' object is not subscriptable" in a function signature
(3 answers)
Closed 2 months ago.
Considering to Python Docs for typing why code below isn't working?
>>> Vector = list[float]
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'type' object is not subscriptable
In docs there is the same example as I mentioned above. here
Vector = list[float]
def scale(scalar: float, vector: Vector) -> Vector:
return [scalar * num for num in vector]
I didn't find question about this example.
The ability to use the [] operator on types like list for type hinting was added in 3.9.
https://docs.python.org/3/whatsnew/3.9.html#type-hinting-generics-in-standard-collections
In earlier versions it will generate the error you describe, and you need to import List object from typing instead.
from typing import List
List[float]

How can I open the underlying script of built-in functions? [duplicate]

This question already has answers here:
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
How can I open the underlying script for built-in functions? For example the function len.
I want to open the script of the len function to study how the underlying code is built, for educational purposes.
I have tried writing open(len) and open(len()), but I only get this error message:
Traceback (most recent call last):
File "C:/Users/someo/PycharmProjects/Test/test.py", line 1, in <module>
open(len())
TypeError: len() takes exactly one argument (0 given)
These definitions can't be accessed, because they aren't part of a package. They live in Python itself, therefore they cannot be accessed. You can't get in by making them raise an error either (len(5) won't let you see inside the function, and will just raise a TypeError). Python itself won't show you the full traceback because these definitions aren't meant to be seen.

TypeError: 'list' object is not callable(python) [duplicate]

This question already has answers here:
Why does "example = list(...)" result in "TypeError: 'list' object is not callable"? [duplicate]
(14 answers)
Closed 3 years ago.
I have a simple script:
list = [1, 2, 3, 4, 5]
myrange = list(range(1, 10))
for number in list:
if number in myrange:
print(number, 'is between 1 and 10')
However, whenever I attempt to run my script, Python raises an error:
Traceback (most recent call last):
File "python", line 2, in <module>
TypeError: 'list' object is not callable
What does this error mean? Why am I getting it? And how can I fix it?
Before you can fully understand what the error means and how to solve, it is important to understand what a built-in name is in Python.
What is a built-in name?
In Python, a built-in name is a name that the Python interpreter already has assigned a predefined value. The value can be either a function or class object. These names are always made available by default, no matter the scope. Some of the values assigned to these names represent fundamental types of the Python language, while others are simple useful.
As of the latest version of Python - 3.6.2 - there are currently 61 built-in names. A full list of the names and how they should be used, can be found in the documentation section Built-in Functions.
An important point to note however, is that Python will not stop you from re-assigning builtin names. Built-in names are not reserved, and Python allows them to be used as variable names as well.
Here is an example using the dict built-in:
>>> dict = {}
>>> dict
{}
>>>
As you can see, Python allowed us to assign the dict name, to reference a dictionary object.
What does "TypeError: 'list' object is not callable" mean?
To put it simply, the reason the error is occurring is because you re-assigned the builtin name list in the script:
list = [1, 2, 3, 4, 5]
When you did this, you overwrote the predefined value of the built-in name. This means you can no longer use the predefined value of list, which is a class object representing Python list.
Thus, when you tried to use the list class to create a new list from a range object:
myrange = list(range(1, 10))
Python raised an error. The reason the error says "'list' object is not callable", is because as said above, the name list was referring to a list object. So the above would be the equivalent of doing:
[1, 2, 3, 4, 5](range(1, 10))
Which of course makes no sense. You cannot call a list object.
How can I fix the error?
If you are getting a similar error such as this one saying an "object is not callable", chances are you used a builtin name as a variable in your code. In this case the fix is as simple as renaming the offending variable. For example, to fix the above code, we could rename our list variable to ints:
ints = [1, 2, 3, 4, 5] # Rename "list" to "ints"
myrange = list(range(1, 10))
for number in ints: # Renamed "list" to "ints"
if number in myrange:
print(number, 'is between 1 and 10')
PEP8 - the official Python style guide - includes many recommendations on naming variables.
This is a very common error new and old Python users make. This is why it's important to always avoid using built-in names as variables such as str, dict, list, range, etc.
Many linters and IDEs will warn you when you attempt to use a built-in name as a variable. If your frequently make this mistake, it may be worth your time to invest in one of these programs.
I didn't rename a built-in name, but I'm still getting "TypeError: 'list' object is not callable". What gives?
Another common cause for the above error is attempting to index a list using parenthesis (()) rather than square brackets ([]). For example:
>>> lst = [1, 2]
>>> lst(0)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
lst(0)
TypeError: 'list' object is not callable
For an explanation of the full problem and what can be done to fix it, see TypeError: 'list' object is not callable while trying to access a list.
Here is the mcve!
>>> []()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Try also {}() and ()(). The message TypeError: 'X' object is not callable means that you wrote expression(some_arguments) where expression is an instance of the X type, and this type doesn't support to be used in a function call syntax. Most of the time you wrote this because you thought expression was a function or some other callable type.

Python Str object is not callable in Spyder [duplicate]

This question already has an answer here:
Builtin function not working with Spyder
(1 answer)
Closed 4 years ago.
I have been trying to use str() function to convert the integer to string in Spyder (python 2.7). Every time I got TypeError: 'str' object is not callable
For example, I wrote this simple code to test it and I got the same error:
x = 5
print str(x)
Can someone help me in this
You have overwritten the built-in str somewhere in your code.
>>> str = 'foo' # overwriting the builtin `str`
>>> x = 5
>>> print str(x) # equivalent to 'foo'(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

Does a variable override method here

I am starting to play around with python a little, and as a novice i tried this:
>>> s="";str=""
>>> for x in [ 1,2,3,4,5,6 ] :
... s += str(x)
...
Traceback (most recent call last):
File "<console>", line 3, in <module>
TypeError: 'str' object is not callable
I accidentally declared a variable called str (str is supposed to be a function).
Assuming it would break the semantics too much, even in a dynamically
typed language, is there a namespace i can use to qualify methods
like str and ensure this does not happen or make it difficult?
This is what import <module> instead of from <module> import * is used for. As long as you use str in the only meaning of local variable value in <module>, you can use
module.str elswhere, without mangling namespace.
The only tokens that can't be clashed are keywords. This is intended functionality and there is no way to prevent this: everything is an object in Python
You might want to use some IDE tools, p.ex. Eclipse+PyDev, that checks your code and warn for possible errors.
As per your question you have already defined str=""
so when you will call str method which converts values into string it will not call actual method in place of that it will call str="".
that's why you are getting error because you can not call a str object to convert int to string.

Categories