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
3
  • 8
    don't name your string variable str you're shadowing the built-in str Commented Jul 12, 2012 at 20:28
  • One of your parameter did not implement str Commented Jul 12, 2012 at 20:32
  • 2
    @Pooya nope. there would be a different error Commented Jul 12, 2012 at 20:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 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.
I call str() on a object created by a zip() function. This does not support the built in __str__() method.
@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.
@nitin Strange. IMHO that should fall back to __repr__() or one of object's methods...
|
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

2 Comments

I cannot find any assignment to str in my function. Is this the only reason for this error>
I changed my code from str(vec) to vec.__str__()... I still get the same err

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.