Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding type in python - TypeError 'unicode' object is not callable

I'm trying to make sure an object is a string type in Python (for google app engine). I'm doing this so that I can change it to a db.Text type if its more than 500 bytes. However, I keep getting the error: TypeError 'unicode' object is not callable

    if type(value) in types.StringTypes and len(value) > 499:
        value = db.Text(value)
    setattr(entity, key, value)

What should I use to test if the type of the object is a string?

like image 672
Chris Dutrow Avatar asked May 09 '11 21:05

Chris Dutrow


2 Answers

I think you just need to remove the parentheses from types.StringTypes, since it is a tuple (and not callable, hence the error). Either that, or your code is actually using StringType, which means your code is making a new string instance instead of returning the str type. Either way, it looks like a typo. See the docs.

like image 87
Greg Haskins Avatar answered Sep 26 '22 07:09

Greg Haskins


Why are you calling types.StringTypes ? It's a tuple:

>>> types.StringTypes
(<type 'str'>, <type 'unicode'>)

Use isinstance(value, types.StringTypes) and len(value) > 499

like image 45
Jochen Ritzel Avatar answered Sep 26 '22 07:09

Jochen Ritzel