Python Str object is not callable in Spyder [duplicate] - python

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

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 reference the type `module` in Python? [duplicate]

This question already has answers here:
Python typing for module type
(1 answer)
Check if a parameter is a Python module?
(6 answers)
Closed 2 years ago.
If I import a module in Python, then the type of the module is, unsurprisingly, module. However, I'm not sure sure how to reference this type directly. Here by "directly" I mean by writing something like module rather than taking the type of a module. For example, in the following shell interaction, I can test whether an object obj is an int by writing isinstance(obj, int), but evidently I cannot test whether obj is a module by writing isinstance(obj, module).
>>> import random
>>> type(random)
<class 'module'>
>>> isinstance(random, module)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'module' is not defined
>>> isinstance(random, 'module')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type or tuple of types
>>> isinstance(random, type(random))
True
>>> isinstance(1, int)
True
>>> isinstance('Hello, World!', str)
True

Why does int.__eq__ seem to be not implemented in python2 [duplicate]

This question already has answers here:
How int() object uses "==" operator without __eq__() method in python2?
(3 answers)
Closed 2 years ago.
I've got a head scratchier and it seems I'm not the only one, but is there really no solution? I find that hard to believe!
So the question is why can't I call int.__eq__ with 2 operators or i.__eq__ with one? How can I use __eq__ (and the other comparison operators) for a per item comparison for a sequence of ints?
Here's a dump from python2.7.17:
>>> i = 0
>>> type(i)
<type 'int'>
>>> i.__eq__(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__eq__'
>>> type(i).__eq__(i, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected 1 arguments, got 2
>>> type(i).__eq__(0)
NotImplemented
But my dumo from python3.6.9 behaves itself:
>>> i = 0
>>> type(i)
<class 'int'>
>>> i.__eq__(0)
True
>>> type(i).__eq__(i, 0)
True
>>> type(i).__eq__(0) # this is not expected to work, but just for the sake of voodoo.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected 1 arguments, got 0
I know python2 is no longer supported but there are a few applications that only use python2 and I would like to make my code backwards compatible anyway.
So anyone out there have a solutuon for hacking the comparison magic operator methods as function calls in python2? I am sure there must be some work around.
It seems there is some information on this. I just read that python2 falls back to using cmp in some cases, while in python3 there is no cmp (or so I read). So I guess the thing to do is not use eq and ne but instead use cmp but I love some additional perspective on this
The general rule is: don't touch dunderscore methods, use functions and operators instead and those will delegate to dunderscore implementation as necessary. In your case you're looking for the == operator or the functional equivalent operator.eq.
from operator import eq
from functools import partial
print eq(1, 2)
f = partial(eq, 1)
print f(2)

TypeError: "'int' object is not callable" in python [duplicate]

This question already has answers here:
Typeerror float is not callable. Im trying to figure out why my elif statement are not callable [closed]
(3 answers)
Closed 6 years ago.
In python I'm getting this error "TypeError: 'int' object is not callable". I've read other posts but I can't figure what why this happening.
import pdb
import numpy
b=int(raw_input("b?"))
a=int(raw_input("a?"))
c=int(raw_input("c?"))
pdb.set_trace()
sqrt= ( (b*b) - (4* (a*c))) /(2*a)
x= -b(numpy.sqrt(sqrt))
print x
Can anyone please tell me what's the problem?
`
This code:
-b(numpy.sqrt(sqrt))
tries to call a function b() and negates the result.
This is what you want:
-b * (numpy.sqrt(sqrt))

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

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.

Categories