确定对象的类型?[重复]
- 2024-12-11 08:47:00
- admin 原创
- 163
问题描述:
有没有简单的方法来确定一个变量是列表、字典还是其他东西?
解决方案 1:
有两个内置函数可帮助您识别对象的类型。type()
如果您需要对象的确切类型,以及isinstance()
要根据某些内容检查对象的类型,则可以使用这两个函数。通常,您会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
解决方案 2:
使用type()
:
>>> a = []
>>> type(a)
<type 'list'>
>>> f = ()
>>> type(f)
<type 'tuple'>
解决方案 3:
try
使用...块可能更符合 Python 风格except
。这样,如果你有一个类,它听起来像列表,或者像字典,那么无论它的实际类型是什么,它都会正常运行。
需要澄清的是,区分变量类型的首选方法是使用鸭子类型:只要变量响应的方法(和返回类型)是子程序所期望的,就按照您期望的方式处理它。例如,如果您有一个类,它使用getattr
and重载括号运算符setattr
,但使用了一些有趣的内部方案,如果它试图模拟字典,那么让它表现为字典是合适的。
检查的另一个问题type(A) is type(B)
是,如果A
是的子类B
,则它会求值为false
,而从编程上讲,您希望它是true
。如果对象是列表的子类,它应该像列表一样工作:检查另一个答案中所示的类型将阻止这种情况。(isinstance
但是会起作用)。
解决方案 4:
在对象实例上你还具有:
__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 和新式类(可选自 Python 2.6)中,类和类型已合并,这有时可能会导致意外结果。主要出于这个原因,我最喜欢的测试类型/类的方法是使用内置函数isinstance 。
解决方案 5:
确定 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
- 根据里氏替换原则,您希望能够使用子类实例而不破坏您的代码 - 并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)
或者干脆不进行显式类型检查
或者,也许最好的方法是使用鸭子类型,而不要显式地对代码进行类型检查。鸭子类型支持里氏替换,更优雅,更简洁。
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
明确检查实际的子类或注册的抽象。只要有意义的事情就避免进行类型检查。
解决方案 6:
您可以使用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
解决方案 7:
小心使用 isinstance
isinstance(True, bool)
True
>>> isinstance(True, int)
True
但输入
type(True) == bool
True
>>> type(True) == int
False
解决方案 8:
使用 type()
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__
它我们可以阅读
解决方案 9:
在很多实际情况中,除了使用type
或 之外,isinstance
还可以使用@functools.singledispatch
,它用于定义泛型函数(由多个针对不同类型实现相同操作的函数组成的函数)。
换句话说,当您有如下代码时您会想要使用它:
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'>
另外,我们可以使用抽象类来同时覆盖几种类型:
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!
解决方案 10:
虽然这些问题已经很老了,但我在自己寻找正确方法时偶然发现了这个问题,我认为它仍然需要澄清,至少对于 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__
方法最接近正确性,但在一个关键情况下它不起作用:当传入的对象是旧式类(而不是实例!)时,因为这些对象缺少这种属性。
这是我能想到的以一致的方式满足这种合理问题的最小的代码片段:
#!/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
解决方案 11:
除了前面的答案之外,值得一提的是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
所有这些的完整列表可在 的文档中查看collections.abc
。
解决方案 12:
value = 12
print(type(value)) # will return <class 'int'> (means integer)
或者你可以做这样的事情
value = 12
print(type(value) == int) # will return true
解决方案 13:
一般来说,你可以从对象中提取一个带有类名的字符串,
str_class = object.__class__.__name__
并用它来进行比较,
if str_class == 'dict':
# blablabla..
elif str_class == 'customclass':
# blebleble..
解决方案 14:
type()
是比更好的解决方案isinstance()
,特别是对于booleans
:
True
和False
只是 Python 中表示1
和的关键字0
。因此,
isinstance(True, int)
和
isinstance(False, int)
都返回True
。两个布尔值都是整数的实例。type()
然而,更聪明:
type(True) == int
返回False
。
解决方案 15:
为了完整性,isinstance不适用于对非实例的子类型的类型检查。虽然这完全合理,但所有答案(包括已接受的答案)均未涵盖这一点。请使用issubclass来实现。
>>> class a(list):
... pass
...
>>> isinstance(a, list)
False
>>> issubclass(a, list)
True