有没有一种简单的方法来确定变量是列表、字典还是其他东西?
try
无济于事。例如,如果您知道用户可以传入字符串或数组,那么两者都是可索引的,但该索引意味着完全不同的东西。在这些情况下仅仅依靠 try-catch 会以意想不到和奇怪的方式失败。一种解决方案是制作一个单独的方法,另一种是添加一点类型检查。我个人更喜欢多态行为而不是做几乎相同事情的多种方法......但这只是我:)
有两个内置函数可以帮助您识别对象的类型。如果您需要对象的确切类型,则可以使用 type()
,并根据某些内容检查对象的类型。通常,您希望在大多数情况下使用 isinstance()
,因为它非常健壮并且还支持类型继承。
要获取对象的实际类型,请使用内置 type()
函数。将对象作为唯一参数传递将返回该对象的类型对象:
>>> type([]) is list
True
>>> type({}) is dict
True
>>> type('') is str
True
>>> type(0) is int
True
这当然也适用于自定义类型:
>>> class Test1 (object):
pass
>>> class Test2 (Test1):
pass
>>> a = Test1()
>>> b = Test2()
>>> type(a) is Test1
True
>>> type(b) is Test2
True
请注意,type()
只会返回对象的直接类型,但无法告诉您有关类型继承的信息。
>>> type(b) is Test1
False
为了解决这个问题,您应该使用 isinstance
函数。这当然也适用于内置类型:
>>> isinstance(b, Test1)
True
>>> isinstance(b, Test2)
True
>>> isinstance(a, Test1)
True
>>> isinstance(a, Test2)
False
>>> isinstance([], list)
True
>>> isinstance({}, dict)
True
isinstance()
通常是确保对象类型的首选方法,因为它也接受派生类型。因此,除非您确实需要类型对象(无论出于何种原因),否则使用 isinstance()
优于 type()
。
isinstance()
的第二个参数也接受类型元组,因此可以一次检查多种类型。如果对象属于以下任何一种类型,isinstance
将返回 true:
>>> isinstance([], (tuple, list, set))
True
使用 type()
:
>>> a = []
>>> type(a)
<type 'list'>
>>> f = ()
>>> type(f)
<type 'tuple'>
使用 try
...except
块可能更符合 Pythonic。那样的话,如果你有一个像列表一样的类,或者像 dict 一样的类,不管它的类型 真正 是什么,它都会正常运行。
澄清一下,在变量类型之间“区分”的首选方法是使用称为 duck typing 的东西:只要变量响应的方法(和返回类型)是您的子例程所期望的,就可以像您期望的那样对待它它是。例如,如果您有一个使用 getattr
和 setattr
重载括号运算符的类,但使用了一些有趣的内部方案,那么如果它试图模拟的话,它会适合用作字典。
type(A) is type(B)
检查的另一个问题是,如果 A
是 B
的子类,它的计算结果为 false
,而您以编程方式希望它是 true
。如果一个对象是列表的子类,它应该像列表一样工作:检查其他答案中显示的类型将防止这种情况发生。 (但是,isinstance
会起作用)。
try
...except
在您想要处理错误时是一个很好的解决方案,但在根据类型决定行为时却不是。
在对象的实例上,您还具有:
__class__
属性。这是取自 Python 3.3 控制台的示例
>>> str = "str"
>>> str.__class__
<class 'str'>
>>> i = 2
>>> i.__class__
<class 'int'>
>>> class Test():
... pass
...
>>> a = Test()
>>> a.__class__
<class '__main__.Test'>
请注意,在 python 3.x 和 New-Style 类(可从 Python 2.6 中选择使用)中,类和类型已被合并,这有时会导致意外结果。主要出于这个原因,我最喜欢的测试类型/类的方法是使用 isinstance 内置函数。
__class__
在 Python 2.x 上基本没问题,Python 中唯一没有 __class__
属性的对象是老式类 AFAIK。顺便说一句,我不理解您对 Python 3 的担忧 - 在这样的版本上,只是每个对象都有一个 __class__
属性指向正确的类。
确定 Python 对象的类型
使用 type
确定对象的类型
>>> obj = object()
>>> type(obj)
<class 'object'>
尽管它有效,但请避免使用像 __class__
这样的双下划线属性 - 它们在语义上不是公共的,而且,虽然在这种情况下可能不是,但内置函数通常具有更好的行为。
>>> obj.__class__ # avoid this!
<class 'object'>
类型检查
有没有一种简单的方法来确定变量是列表、字典还是其他东西?我得到了一个可能是任何一种类型的对象,我需要能够区分它们。
好吧,这是一个不同的问题,不要使用类型 - 使用 isinstance
:
def foo(obj):
"""given a string with items separated by spaces,
or a list or tuple,
do something sensible
"""
if isinstance(obj, str):
obj = str.split()
return _foo_handles_only_lists_or_tuples(obj)
这涵盖了您的用户可能通过子类化 str
来做一些聪明或明智的事情 - 根据 Liskov Substitution 的原则,您希望能够在不破坏代码的情况下使用子类实例 - 而 isinstance
支持这一点。
使用抽象
更好的是,您可以从 collections
或 numbers
中寻找特定的抽象基类:
from collections import Iterable
from numbers import Number
def bar(obj):
"""does something sensible with an iterable of numbers,
or just one number
"""
if isinstance(obj, Number): # make it a 1-tuple
obj = (obj,)
if not isinstance(obj, Iterable):
raise TypeError('obj must be either a number or iterable of numbers')
return _bar_sensible_with_iterable(obj)
或者只是不要明确类型检查
或者,也许最好的方法是使用鸭子类型,并且不要显式地对您的代码进行类型检查。 Duck-typing 支持 Liskov Substitution,更优雅,更简洁。
def baz(obj):
"""given an obj, a dict (or anything with an .items method)
do something sensible with each key-value pair
"""
for key, value in obj.items():
_baz_something_sensible(key, value)
结论
使用 type 来实际获取实例的类。
使用 isinstance 显式检查实际的子类或注册的抽象。
并且避免在有意义的地方进行类型检查。
try
/except
而不是显式检查。
您可以使用 type()
或 isinstance()
。
>>> type([]) is list
True
请注意,您可以通过在当前范围内分配同名变量来破坏 list
或任何其他类型。
>>> 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
dict([("key1", "value1"), ("key2", "value2")])
),您的 dict
字符串也会失败。这类问题的答案是 "Then don't do that"。不要隐藏内置类型名称并期望事情正常工作。
小心使用 isinstance
isinstance(True, bool)
True
>>> isinstance(True, int)
True
但是输入
type(True) == bool
True
>>> type(True) == int
False
虽然问题已经很老了,但我在自己找到正确方法时偶然发现了这个问题,我认为它仍然需要澄清,至少对于 Python 2.x (没有检查 Python 3,但由于经典类出现问题在这样的版本上没有了,可能没关系)。
在这里,我试图回答标题的问题:如何确定任意对象的类型?在许多评论和答案中,关于使用或不使用 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__
方法最接近正确性,但它在一个关键情况下不起作用:当传入的对象是旧式 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
的存在,它包含几个补充鸭子类型的抽象基类 (ABC)。
例如,不是显式检查某物是否是一个列表:
isinstance(my_obj, list)
如果您只想查看您拥有的对象是否允许获取项目,您可以使用 collections.abc.Sequence
:
from collections.abc import Sequence
isinstance(my_obj, Sequence)
如果您对允许获取、设置和删除项目(即可变序列)的对象非常感兴趣,那么您会选择collections.abc.MutableSequence
。
那里定义了许多其他 ABC,Mapping
用于可用作地图的对象,Iterable
、Callable
等等。在 the documentation for collections.abc
. 中可以看到所有这些的完整列表
在许多实际情况下,除了使用 type
或 isinstance
,您还可以使用 @functools.singledispatch
,它用于定义 generic functions(由多个函数组成的函数,为不同类型实现相同的操作 )。
换句话说,当你有如下代码时,你会想要使用它:
def do_something(arg):
if isinstance(arg, int):
... # some code specific to processing integers
if isinstance(arg, str):
... # some code specific to processing strings
if isinstance(arg, list):
... # some code specific to processing lists
... # etc
这是它如何工作的一个小例子:
from functools import singledispatch
@singledispatch
def say_type(arg):
raise NotImplementedError(f"I don't work with {type(arg)}")
@say_type.register
def _(arg: int):
print(f"{arg} is an integer")
@say_type.register
def _(arg: bool):
print(f"{arg} is a boolean")
>>> say_type(0)
0 is an integer
>>> say_type(False)
False is a boolean
>>> say_type(dict())
# long error traceback ending with:
NotImplementedError: I don't work with <class 'dict'>
另外,我们可以使用 abstract classes 一次涵盖多种类型:
from collections.abc import Sequence
@say_type.register
def _(arg: Sequence):
print(f"{arg} is a sequence!")
>>> say_type([0, 1, 2])
[0, 1, 2] is a sequence!
>>> say_type((1, 2, 3))
(1, 2, 3) is a sequence!
value = 12
print(type(value)) # will return <class 'int'> (means integer)
或者你可以做这样的事情
value = 12
print(type(value) == int) # will return true
使用类型()
x='hello this is a string'
print(type(x))
输出
<class 'str'>
只提取 str 使用这个
x='this is a string'
print(type(x).__name__)#you can use__name__to find class
输出
str
如果您使用 type(variable).__name__
,我们可以阅读它
type()
是比 isinstance()
更好的解决方案,尤其是对于 booleans
:
True
和 False
只是在 python 中表示 1
和 0
的关键字。因此,
isinstance(True, int)
和
isinstance(False, int)
两者都返回 True
。两个布尔值都是整数的实例。然而,type()
更聪明:
type(True) == int
返回 False
。
通常,您可以从具有类名的对象中提取字符串,
str_class = object.__class__.__name__
并将其用于比较,
if str_class == 'dict':
# blablabla..
elif str_class == 'customclass':
# blebleble..
为了完整起见, isinstance 将 不适用于非实例的子类型的类型检查。虽然这完全有道理,但没有一个答案(包括接受的答案)涵盖它。为此使用 issubclass。
>>> class a(list):
... pass
...
>>> isinstance(a, list)
False
>>> issubclass(a, list)
True
不定期副业成功案例分享
is
而不是==
更清楚,因为类型是单例isinstance
是首选形式,因此不需要使用==
或is
。type
是最佳答案。有时isinstance
是最佳答案,有时鸭子打字是最佳答案。了解所有选项很重要,这样您就可以选择更适合情况的选项。type(foo) is SomeType
比isinstance(foo, SomeType)
更好的情况。isinstance
用于你的用例(检查一系列类型) ,并且语法也一样干净,它具有可以捕获子类的巨大优势。使用OrderedDict
的人会讨厌您的代码失败,因为它只接受纯字典。