Python string object not callable

I can’t understand why I get Type Error for the next statement

log.debug('vec : %s blasted : %s\n' %(str(vec), str(bitBlasted)))

type(vec)  is unicode
bitBlasted is a list

I get the following error

TypeError: 'str' object is not callable
+7
source share
2 answers

Built-in shading

Or, as Colleen said , you can obscure the built-in str:

>>> str = some_variable_or_string #this is wrong
>>> str(123.0) #Or this will happen
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

One solution would be to change the name of the variable to str_or something else. The best solution would be to avoid such a Hungarian naming system - this is not Java, use Python polymorphism in its entirety, and use a more descriptive name instead.

The correct method is not defined

Another possibility is that the object may not have the proper method __str__or even one.

, Python str: -

  • __str__
  • __str__
  • __repr__
  • __repr__
  • : <module>.<classname> instance at <address>, <module> self.__class__.__module__, <classname> is self.__class__.__name__ <address> is id(self)

, __str__, __unicode__ ( Python 3.x, __bytes__ __str__. __str__ :

class foo:
    ...
    def __str__(self):
        return unicode(self).encode('utf-8')

. .

+7

, str - . str . :

>>> mynum = 123
>>> print str(mynum)
123
>>> str = 'abc'
>>> print str(mynum)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
+5

All Articles