7

I am not able to understand why I am getting a Type Error for the following statement

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

type(vec)  is unicode
bitBlasted is a list

I am getting the following error

TypeError: 'str' object is not callable
nitin
  • 7,234
  • 11
  • 39
  • 53

2 Answers2

7

Shadowing the built-in

Either as Collin said, you could be shadowing 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 variable name to str_ or something. A better solution would be to avoid this kind of Hungarian naming system -- this isn't Java, use Python's polymorphism to its fullest and use a more descriptive name instead.

Not defining a proper method

Another possibility is that the object may not have a proper __str__ method or even one at all.

The way Python checks for the str method is:-

  • the __str__ method of the class
  • the __str__ method of its parent class
  • the __repr__ method of the class
  • the __repr__ method of its parent class
  • and the final fallback: a string in form of <module>.<classname> instance at <address> where <module> is self.__class__.__module__, <classname> is self.__class__.__name__ and <address> is id(self)

Even better than __str__ would be to use the new __unicode__ method (in Python 3.x, they're __bytes__ and __str__. You could then implement __str__ as a stub method:

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

See this question for more details.

Community
  • 1
  • 1
Yatharth Agarwal
  • 4,385
  • 2
  • 24
  • 53
  • 1
    after spending a lot of time going thru my code.. I did find a place where I was calling str to an object that does not have proper __str__ defined... thanks – nitin Jul 12 '12 at 23:32
  • 1
    @nitin so did you shadown str, or did you call str() on some object that doesn't support it? Please let us know what the object was. – Colin Dunklau Jul 12 '12 at 23:59
  • I call str() on a object created by a zip() function. This does not support the built in __str__() method. – nitin Jul 13 '12 at 06:48
  • @nitin Don't `zip` functions just return an iterable (most likely a list or tuple)? They shouldn't have any problem with their `str` method. – Yatharth Agarwal Jul 13 '12 at 07:37
  • @nitin Strange. IMHO that should fall back to `__repr__()` or one of `object`'s methods... – glglgl Jul 13 '12 at 07:37
  • @nitin Revised my answer, thanks for accepting. Could we have a look at the code, please?? I don't think `zip` should be causing any problems... – Yatharth Agarwal Jul 13 '12 at 10:42
5

As mouad said, you've used the name str somewhere higher in the file. That shadows the existing built-in str, and causes the error. For example:

>>> 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
Colin Dunklau
  • 3,001
  • 1
  • 20
  • 19