Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 确定对象的类型?_Python_Dictionary_Types_Typeof - Fatal编程技术网

Python 确定对象的类型?

Python 确定对象的类型?,python,dictionary,types,typeof,Python,Dictionary,Types,Typeof,有没有一种简单的方法来确定变量是列表、字典还是其他?我正在取回一个对象,该对象可能是任何一种类型,我需要能够分辨出它们之间的区别。您可以使用type()来执行此操作: >a=[] >>>类型(a) >>>f=() >>>类型(f) 有两个内置函数可帮助您识别对象的类型。如果需要对象的确切类型,可以使用,并根据某些内容检查对象的类型。通常,大多数情况下都需要使用isinstance(),因为它非常健壮,并且还支持类型继承 >>> type(b) is Test1 False

有没有一种简单的方法来确定变量是列表、字典还是其他?我正在取回一个对象,该对象可能是任何一种类型,我需要能够分辨出它们之间的区别。

您可以使用
type()
来执行此操作:

>a=[]
>>>类型(a)
>>>f=()
>>>类型(f)

有两个内置函数可帮助您识别对象的类型。如果需要对象的确切类型,可以使用,并根据某些内容检查对象的类型。通常,大多数情况下都需要使用
isinstance()
,因为它非常健壮,并且还支持类型继承

>>> type(b) is Test1
False

要获取对象的实际类型,可以使用内置函数。将对象作为唯一参数传递将返回该对象的类型对象:

>>> 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(b, Test1)
True
>>> isinstance(b, Test2)
True
>>> isinstance(a, Test1)
True
>>> isinstance(a, Test2)
False
>>> isinstance([], list)
True
>>> isinstance({}, dict)
True
isinstance()
通常是确保对象类型的首选方法,因为它也将接受派生类型。因此,除非您确实需要type对象(出于任何原因),否则使用
isinstance()
比使用
type()
更可取

isinstance()
的第二个参数也接受类型的元组,因此可以一次检查多个类型<如果对象属于以下任何类型,则代码>isinstance将返回true:

>>> isinstance([], (tuple, list, set))
True

使用
try
可能更像python,除了
块。这样,如果你有一个像列表一样嘎嘎作响的类,或者像dict一样嘎嘎作响的类,那么不管它的类型是什么,它都会正常运行

为了澄清这一点,在变量类型之间“区分”的首选方法是:只要变量响应的方法(和返回类型)是您的子例程所期望的,就按您所期望的那样对待它。例如,如果有一个类使用
getattr
setattr
重载方括号运算符,但使用了一些有趣的内部模式,那么如果它正试图模拟这样的模式,则将其用作字典是合适的


类型(A)的另一个问题是类型(B)
检查是,如果
A
B
的子类,则它的计算结果为
false
,而从编程角度来说,您希望它是
true
。如果一个对象是一个列表的子类,它应该像一个列表一样工作:检查另一个答案中显示的类型可以防止这种情况。(
isinstance
将起作用)。

在对象的实例上,您还具有:

\u类__
属性。下面是一个取自Python3.3控制台的示例

>>str=“str”
>>>str._类__
>>>i=2
>>>我上了一节课__
>>>类测试():
...     通过
...
>>>a=测试()
>>>一节课__
请注意,在Python3.x和新样式的类(可选来自Python2.6)中,类和类型已经合并,这有时会导致意外的结果。主要由于这个原因,我最喜欢的测试类型/类的方法是使用内置函数。

您可以使用
type()
isinstance()

请注意,您可以通过在当前范围内指定同名变量来关闭
列表
或任何其他类型

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

虽然这些问题已经很老了,但我自己在找到一个合适的方法时偶然发现了这一点,我认为它仍然需要澄清,至少对于Python2.x来说是如此(没有检查Python3,但是由于这个问题出现在这样的版本中的经典类上,这可能并不重要)

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

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
我认为,这不是大多数人所期望的

\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

这是我能想到的最小的代码片段,它以一致的方式满足了这样一个合法的问题:

#!/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

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

例如,不是显式检查某个内容是否是具有以下内容的列表:

isinstance(my_obj, list)
如果您只想查看您拥有的对象是否允许获取项目,可以使用:

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

>>> type([]) is list
True
这里定义了许多其他的ABC,
映射
用于可以用作映射的对象,
可调用
可调用
,等等。在

确定Python对象的类型中可以看到所有这些内容的完整列表 使用
type

>>> obj = object()
>>> type(obj)
<class 'object'>
类型检查 有没有一种简单的方法来确定变量是列表、字典还是其他?我得到了一个对象,可能是任何一种类型,我需要能够区分的差异

这是另一个问题,不要使用type-use

>>> obj = object()
>>> type(obj)
<class 'object'>
>>> obj.__class__ # avoid this!
<class 'object'>
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)
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)
isinstance(True, bool)
True
>>> isinstance(True, int)
True
type(True) == bool
True
>>> type(True) == int
False
isinstance(True, int)
isinstance(False, int)
type(True) == int
str_class = object.__class__.__name__
if str_class == 'dict':
    # blablabla..
elif str_class == 'customclass':
    # blebleble..
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")
from collections.abc import Sequence


@say_type.register
def _(arg: Sequence):
    print(f"{arg} is a sequence!")
>>> class a(list):
...   pass
... 
>>> isinstance(a, list)
False
>>> issubclass(a, list)
True
value = 12
print(type(value)) # will return <class 'int'> (means integer)
value = 12
print(type(value) == int) # will return true