在python中查找类型 - TypeError'unicode'对象不可调用

Chr*_*row 8 python google-app-engine types

我正在尝试确保对象是Python中的字符串类型(对于谷歌应用引擎).我正在这样做,以便我可以将其更改为db.Text类型,如果它超过500字节.但是,我一直收到错误:TypeError 'unicode' object is not callable

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

如果对象的类型是字符串,我应该用什么来测试?

Gre*_*ins 6

我认为你只需要删除括号types.StringTypes,因为它是一个元组(并且不可调用,因此错误).要么,或者您的代码实际上正在使用StringType,这意味着您的代码正在创建一个新的字符串实例而不是返回该str类型.无论哪种方式,它看起来像一个错字.查看文档.


Joc*_*zel 4

你为什么打电话types.StringTypes?这是一个元组:

>>> types.StringTypes
(<type 'str'>, <type 'unicode'>)
Run Code Online (Sandbox Code Playgroud)

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