有没有一种简单的方法来确定变量是列表、字典还是其他什么?


当前回答

使用类型():

>>> a = []
>>> type(a)
<type 'list'>
>>> f = ()
>>> type(f)
<type 'tuple'>

其他回答

type()是比isinstance()更好的解决方案,尤其是对于布尔值:

True和False只是python中表示1和0的关键字。因此

isinstance(True, int)

and

isinstance(False, int)

两者都返回True。两个布尔值都是整数的实例。然而,type()更聪明:

type(True) == int

返回False。

通常,您可以从具有类名的对象中提取字符串,

str_class = object.__class__.__name__

并将其用于比较,

if str_class == 'dict':
    # blablabla..
elif str_class == 'customclass':
    # blebleble..

虽然这些问题很古老,但我在自己找到正确的方法时偶然发现了这一点,我认为它仍然需要澄清,至少对于Python2.x是如此(没有检查Python3,但由于这个问题出现在此类版本中的经典类中,这可能无关紧要)。

在这里,我试图回答标题的问题:如何确定任意对象的类型?其他关于使用或不使用isinstance的建议在许多评论和回答中都很好,但我并没有解决这些问题。

type()方法的主要问题是它不能在旧样式实例中正常工作:

class One:
    pass

class Two:
    pass


o = One()
t = Two()

o_type = type(o)
t_type = type(t)

print "Are o and t instances of the same class?", o_type is t_type

执行此代码段将产生:

Are o and t instances of the same class? True

我认为,这并不是大多数人所期望的。

__class__方法最接近正确性,但在一个关键的情况下,它不起作用:当传入的对象是旧式类(不是实例!)时,因为这些对象缺少这种属性。

这是我能想到的以一致方式满足此类合法问题的最小代码片段:

#!/usr/bin/env python
from types import ClassType
#we adopt the null object pattern in the (unlikely) case
#that __class__ is None for some strange reason
_NO_CLASS=object()
def get_object_type(obj):
    obj_type = getattr(obj, "__class__", _NO_CLASS)
    if obj_type is not _NO_CLASS:
        return obj_type
    # AFAIK the only situation where this happens is an old-style class
    obj_type = type(obj)
    if obj_type is not ClassType:
        raise ValueError("Could not determine object '{}' type.".format(obj_type))
    return obj_type

除了前面的答案之外,值得一提的是collections.abc的存在,它包含几个补充duck类型的抽象基类(abc)。

例如,不必显式检查某项内容是否为列表,而是:

isinstance(my_obj, list)

如果您只想查看所拥有的对象是否允许获取项目,可以使用collections.abc.Sequence:

from collections.abc import Sequence
isinstance(my_obj, Sequence) 

如果您对允许获取、设置和删除项目(即可变序列)的对象非常感兴趣,那么您可以选择collections.abc.MutableSequence。

这里还定义了许多其他ABC,可以用作映射的对象的映射、Iterable、Callable等。所有这些的完整列表可以在collections.abc的文档中看到。

可以使用type()或isinstance()。

>>> type([]) is list
True

请注意,通过在当前作用域中分配同名变量,可以删除列表或任何其他类型。

>>> the_d = {}
>>> t = lambda x: "aight" if type(x) is dict else "NOPE"
>>> t(the_d) 'aight'
>>> dict = "dude."
>>> t(the_d) 'NOPE'

上面我们看到dict被重新分配给一个字符串,因此测试:

type({}) is dict

…失败。

要解决此问题并更谨慎地使用type():

>>> import __builtin__
>>> the_d = {}
>>> type({}) is dict
True
>>> dict =""
>>> type({}) is dict
False
>>> type({}) is __builtin__.dict
True