在python中查找类型-TypeError'unicode'对象不是callab

2024-04-23 06:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图确保一个对象是Python中的字符串类型(对于google app engine)。我这样做是为了在超过500字节的情况下将其更改为db.Text类型。但是,我一直得到错误:TypeError 'unicode' object is not callable

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

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


Tags: 对象字符串textapp类型db字节object
3条回答

我认为您只需要从types.StringTypes中删除括号,因为它是一个元组(不可调用,因此出错)。或者,或者您的代码实际上使用了StringType,这意味着您的代码正在创建一个新的字符串实例,而不是返回str类型。不管怎样,它看起来像是一个打字错误。请参阅docs

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

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

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

格雷格·哈斯金斯是对的

>>> 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'>)

你能做吗

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

相关问题 更多 >