I'm trying to do the following:
for index, image_properties in enumerate(list_of_properties):
index = str(index)
But I keep getting
TypeError at /
'str' object is not callable
What is going wrong here?
As the commenters have mentioned, you must have str defined somewhere and it overrides the str built-in function.
In Python you can easily "re-bind" symbols like this. See this session for example:
>>> str(2)
'2'
>>> def str(x): return x + 1
...
>>> str(2)
3
>>> str = 1
>>> str(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Moreover the text of your TypeError suggests that str was defined to be a string object somewhere earlier.
Related
I have the following python statement
x = lambda :8()
checking the type of x returns the following
<class 'function'>
but then doing this
x()
TypeError: 'int' object is not callable
I can solve this by putting parenthesis around the lambda like so
x = (lambda :8)()
But I am wondering what is going on.
The problem does not lie with calling x, you are trying to call 8 with 8(). Calling an integer raises an error because instances of int are not callable.
I can solve this by putting parenthesis around the lambda like so
What you are doing with x = (lambda :8)() is construct an anonymous function that always returns the number 8, then call it, and assign the name x to the return value.
>>> x = (lambda :8)()
>>> x
8
However, x() will still raise an error because again it's trying to call an integer.
>>> x()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
a = 3
def f(x):
x = (x**3-4*x)/(3(x**2)-4)
return x
while True:
print(a)
a = f(a)
I'm getting a type error here, and I'm not sure why. I'm trying to run this recursive function, is there any way to fix this?
You need a * operator after your parentheses. Multiplication is only implied in mathematical notation in this context, in Python it looks like you're trying to call a function.
3(x**2)
So it would be
3*(x**2)
For example
>>> 3(5*2)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
3(5*2)
TypeError: 'int' object is not callable
>>> 3*(5*2)
30
i = 0x0800
What I understand here is that 0x0800 is a hexadecimal number where '0x' denotes the hex type and the following number '0800' is a 2 byte hexadecimal number. On assigning it to a variable 'i', when its type is checked I got this error
>>> type(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Here I make out that 'i' is suppose to be an int object. I got more confused when I tried this
>>> print i
2048
What is '2048' exactly .. Can anyone throw some light here ?
i is an integer, but you redefined type:
>>> i = 0x0800
>>> i
2048
>>> type(i)
<type 'int'>
>>> type = 42
>>> type(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del type
>>> type(i)
<type 'int'>
Note the type = 42 line; I created a new global name type and that is found before the built-in. You could also use import __builtin__; __builtin__.type(i) in Python 2, or import builtins; builtins.type(i) in Python 3 to access the original built-in type() function:
>>> import __builtin__
>>> type = 42
>>> __builtin__.type(type)
<type 'int'>
>>> type(type)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del type
>>> type(type)
<type 'type'>
The 0x notation is just one of several ways of specifying an integer literal. You are still producing a regular integer, only the syntax for how you define the value differs here. All of the following notations produce the exact same integer value:
0x0800 # hexadecimal
0o04000 # octal, Python 2 also accepts 0400
0b100000000000 # binary
2048 # decimal
See the Integer Literals reference documentation.
I'll quickly put the answer that I figured out ....
i = 0x0800 will assign an int equivalent of the hexadecimal number (0800) to i.
So if we go down to pieces this looks like
>>> i
2048
>>>
>>> (pow(16,3) * 0) + ( pow(16,2) * 8 ) + (pow (16,1) * 0 ) + (pow(16,0) * 0)
2048
I'm working on a simple size() method while working with Mutablelists and I keep getting the following error:
>>> xs = MutableList
>>> xs
<class __main__.MutableList at 0x02AC6848>
>>> xs.size()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
xs
.size()
File "C:\Users\safim\Desktop\Python HW 4\a3_1.py", line 59, in size
for x in self :
TypeError: iteration over non-sequence
The code I used was:
result = 0
for x in self :
result + 1
return result
I appreciate the help in advance.
xs is the same object as MutableList, because you made it so:
xs = MutableList
The message printed even tells you this:
<class __main__.MutableList at 0x02AC6848>
As it says, xs is the class, not an instance of that class.
You can't call MutableList.size() (which is what you're trying to do, because xs and MutableList are the same thing) because that doesn't tell it what instance you want to use.
Did you mean to instantiate a MutableList? If so:
xs = MutableList()
Your other code won't work either, since result + 1 adds 1 to result and then throws away that number (you never assign it to a variable). Most likely you mean result += 1.
I copied this code from a book:
lyst = {'Hi':'Hello'}
def changeWord(sentence):
def getWord(word):
lyst.get(word, word)
return ''.join(map(getWord, sentence.split()))
I get this error:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
cambiaPronome('ciao')
File "C:\Python33\2.py", line 6, in cambiaPronome
return ''.join(map(getWord, list(frase)))
TypeError: sequence item 0: expected str instance, NoneType found
What is wrong?
The getWord function doesn't explicitly return anything, so it implicitly returns None. Try returning something, e.g.:
def getWord(word):
return lyst.get(word, word)
The problem is you're trying to write to a string a type which is NoneType. That's not allowed.
If you're interested in getting the None values as well one of the things you can do is convert them to strings.
And you can do it with a list comprehension, like:
return ''.join([str(x) for x in map(getWord, sentence.split())])
But to do it properly in this case, you have to return something on the inner function, else you have the equivalent to return None