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

178 views
Skip to first unread message

Chris Dutrow via StackOverflow

unread,
Jan 19, 2015, 6:34:48 PM1/19/15
to google-appengin...@googlegroups.com

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?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/5942624/finding-type-in-python-typeerror-unicode-object-is-not-callable

Prashant Kumar via StackOverflow

unread,
Jan 19, 2015, 6:34:50 PM1/19/15
to google-appengin...@googlegroups.com

I prefer isinstance(object_to_be_checked, str)



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/5942624/finding-type-in-python-typeerror-unicode-object-is-not-callable/5942675#5942675

Greg Haskins via StackOverflow

unread,
Jan 19, 2015, 6:34:52 PM1/19/15
to google-appengin...@googlegroups.com

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.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/5942624/finding-type-in-python-typeerror-unicode-object-is-not-callable/5942685#5942685

inspectorG4dget via StackOverflow

unread,
Jan 19, 2015, 6:34:54 PM1/19/15
to google-appengin...@googlegroups.com

Greg Haskins is right

>>> types.StringTypes()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable
>>> types.StringTypes
(<type 'str'>, <type 'unicode'>)

Could you do

if type(variable_name) == type("")


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/5942624/finding-type-in-python-typeerror-unicode-object-is-not-callable/5942686#5942686

Jochen Ritzel via StackOverflow

unread,
Jan 19, 2015, 6:34:56 PM1/19/15
to google-appengin...@googlegroups.com

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



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/5942624/finding-type-in-python-typeerror-unicode-object-is-not-callable/5942700#5942700

GreenMatt via StackOverflow

unread,
Jan 19, 2015, 6:34:58 PM1/19/15
to google-appengin...@googlegroups.com

Edit: Oops! I was looking at types.StringType, instead of types.StringTypes, so the test could be:

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

Where the main problem with the OP code was the presence of the parens at the end of types.StringTypes.

Original: A few problems here:

  • If value holds a Unicode, testing it with type() will return types.UnicodeType
  • The types.StringType is a constant, not a function, so drop the parentheses.
  • Furthermore, types.StringType isn't iterable, so test against it with == or is.

Thus, your test could look something like:

if ((type(value) == types.StringType) or (type(value) == types.UnicodeType)) and len(value) > 499:
    value = db.Text(value)


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/5942624/finding-type-in-python-typeerror-unicode-object-is-not-callable/5942719#5942719
Reply all
Reply to author
Forward
0 new messages