确定对象的类型?

Jus*_*ier 1700 python

有一种简单的方法可以确定变量是列表,字典还是其他什么?我得到的对象可能是任何一种类型,我需要能够分辨出来.

pok*_*oke 1902

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

>>> type([]) is list
True
>>> type({}) is dict
True
>>> type('') is str
True
>>> type(0) is int
True
>>> type({})
<type 'dict'>
>>> type([])
<type 'list'>
Run Code Online (Sandbox Code Playgroud)

这当然也适用于自定义类型:

>>> class Test1 (object):
        pass
>>> class Test2 (Test1):
        pass
>>> a = Test1()
>>> b = Test2()
>>> type(a) is Test1
True
>>> type(b) is Test2
True
Run Code Online (Sandbox Code Playgroud)

请注意,type()它只返回对象的直接类型,但无法告诉您类型继承.

>>> type(b) is Test1
False
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,您应该使用该isinstance功能.这当然也适用于内置类型:

>>> isinstance(b, Test1)
True
>>> isinstance(b, Test2)
True
>>> isinstance(a, Test1)
True
>>> isinstance(a, Test2)
False
>>> isinstance([], list)
True
>>> isinstance({}, dict)
True
Run Code Online (Sandbox Code Playgroud)

isinstance()通常是确保对象类型的首选方法,因为它也会接受派生类型.因此,除非您确实需要类型对象(无论出于何种原因),否则isinstance()优先使用type().

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

>>> isinstance([], (tuple, list, set))
True
Run Code Online (Sandbox Code Playgroud)

  • 我认为使用`is`代替`=='更清楚,因为类型是单例 (64认同)
  • @Mike Graham,有时候`type`是最好的答案.有些时候`isinstance`是最好的答案,有时鸭打字是最好的答案.了解所有选项非常重要,这样您就可以选择哪种更适合这种情况. (22认同)
  • @gnibbler,在你将要进行类型检查的情况下(你不应该开始做),`isinstance`无论如何都是首选形式,因此不需要使用`==`或`is`. (18认同)
  • @gnibbler,这可能是,虽然我还没有遇到`type(foo)是SomeType`会比`isinstance(foo,SomeType)`更好的情况. (6认同)
  • @poke:我完全赞同PEP8,但是你在这里攻击一个稻草人:Sven的论点的重要部分不是PEP8,而是你可以使用`isinstance`作为你的用例(检查一系列类型) ,并且语法清晰,具有捕获子类的巨大优势.使用`OrderedDict`的人会讨厌你的代码失败,因为它只接受纯粹的dicts. (5认同)
  • @poke:也许我参加派对有点晚了,但我认为你不应该更喜欢`type(x)是X`而不是`isinstance(x,X)`.甚至你上一篇评论中的例子也更好地写成`isinstance(param,(list,tuple))`.PEP 8明确地不鼓励使用`type(x)是X`. (2认同)
  • 来自CPython实现的示例:[`collections.namedtuple`](https://github.com/python/cpython/blob/e4c06bcca358c6dcb6393a75a1589ff6a2d45cde/Lib/collections/__init__.py#L398):`type(name)是str`作为属性不是字符串(确切)的名称没有意义,可能是一个错误.但是要检查`int`s不是`bool`s,你会做`isinstance(x,int)而不是isinstance(x,bool)`,以便不排除`int`子类,而是排除` bool`.请注意,`bool`不是子类,因此`type(x)不是bool`的工作方式相同,但这样会不一致. (2认同)

ink*_*dmn 157

你可以这样做type():

>>> a = []
>>> type(a)
<type 'list'>
>>> f = ()
>>> type(f)
<type 'tuple'>
Run Code Online (Sandbox Code Playgroud)


Set*_*son 40

使用try... except块可能更Pythonic .这样一来,如果你有这叫声也像列表,或叫声也像字典类,它会循规蹈矩,无论什么的类型真的是.

为了澄清,变量类型之间"区分"的首选方法是使用称为duck typing的东西:只要变量响应的方法(和返回类型)是您的子例程所期望的,就像对待它一样对待它成为.例如,如果你有一个使用getattr和重载括号运算符的类setattr,但是使用了一些有趣的内部方案,那么如果它正在尝试模拟它,那么它应该像字典一样运行.

type(A) is type(B)检查的另一个问题是,如果A是子类B,它会以false编程方式评估您希望它的时间true.如果一个对象是列表的子类,它应该像列表一样工作:检查另一个答案中显示的类型将阻止这种情况.(但isinstance会工作).

  • 尽管如此,鸭子打字并不是真正讲述差异.它是关于使用通用接口. (16认同)
  • 注意 - 大多数编码样式指南建议不要将异常处理作为代码的正常控制流程的一部分,通常是因为它使代码难以阅读.`try` ...`除了`是一个很好的解决方案,当你想要处理错误时,而不是在决定基于类型的行为时. (5认同)

Lor*_*tti 33

在对象的实例上你也有:

__class__
Run Code Online (Sandbox Code Playgroud)

属性.以下是从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'>
Run Code Online (Sandbox Code Playgroud)

请注意,在python 3.x和New-Style类中(可选择从Python 2.6中提供),类和类型已合并,这有时会导致意外结果.主要是因为这个原因,我最喜欢的测试类型/类的方法是在函数内置的isinstance.

  • 你最后的观点非常重要.type(obj)是Class无法正常工作,但isinstance做了伎俩.我知道isinstance无论如何都是首选,但它比仅仅检查派生类型更有利,正如接受的答案中所建议的那样. (2认同)

Aar*_*all 21

确定Python对象的类型

确定对象的类型 type

>>> obj = object()
>>> type(obj)
<class 'object'>
Run Code Online (Sandbox Code Playgroud)

尽管它有效,但避免使用双下划线属性,例如__class__- 它们在语义上不公开,并且虽然在这种情况下可能不是这样,但内置函数通常具有更好的行为.

>>> obj.__class__ # avoid this!
<class 'object'>
Run Code Online (Sandbox Code Playgroud)

类型检查

有一种简单的方法可以确定变量是列表,字典还是其他什么?我得到的对象可能是任何一种类型,我需要能够分辨出来.

那是一个不同的问题,不要使用类型 - 使用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)
Run Code Online (Sandbox Code Playgroud)

这涵盖了用户可能通过子类str化做一些聪明或明智的事情- 根据Liskov Substitution的原则,您希望能够在不破坏代码的情况下使用子类实例 - 并isinstance支持这一点.

使用抽象

更妙的是,你可能会寻找一个特定的抽象基类collectionsnumbers:

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)
Run Code Online (Sandbox Code Playgroud)

或者只是不要明确地进行类型检查

或者,也许最重要的是,使用duck-typing,并且不要显式地键入 - 检查您的代码.鸭子打字支持Liskov替换更优雅和更少的冗长.

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)
Run Code Online (Sandbox Code Playgroud)

结论

  • 使用type真正得到一个实例的类.
  • 使用isinstance显式检查实际的子类或注册的抽象.
  • 并且避免在有意义的地方进行类型检查.


dee*_*392 13

你可以使用type()isinstance().

>>> type([]) is list
True
Run Code Online (Sandbox Code Playgroud)

list通过在同名的当前范围内分配变量,警告您可以使用clobber 或任何其他类型.

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

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

type({}) is dict
Run Code Online (Sandbox Code Playgroud)

...失败.

为了解决这个问题并type()谨慎使用:

>>> import __builtin__
>>> the_d = {}
>>> type({}) is dict
True
>>> dict =""
>>> type({}) is dict
False
>>> type({}) is __builtin__.dict
True
Run Code Online (Sandbox Code Playgroud)

  • 我同意你的"不要那样做"部分.但实际上要告诉某人不要做某事你至少应该解释为什么不这样做,我认为这是一个相关的机会.我的意思是谨慎的方法看起来丑陋,并说明为什么他们可能不想这样做,让他们决定. (3认同)
  • 我不确定是否有必要指出,隐藏内置数据类型的名称对这种情况不利。您的`dict`字符串也会因其他许多代码而失败,例如`dict([(“ key1”,“ value1”),(“ key2”,“ value2”)]))。这类问题的答案是[“那就别那么做”](http://www.youtube.com/watch?v=zCh7z5EwYF8)。不要遮盖住内置类型名称,不要期望一切正常。 (2认同)

小智 8

使用类型()

x='hello this is a string'
print(type(x))
Run Code Online (Sandbox Code Playgroud)

输出

<class 'str'>
Run Code Online (Sandbox Code Playgroud)

仅提取 str 使用此

<class 'str'>
Run Code Online (Sandbox Code Playgroud)

输出

str
Run Code Online (Sandbox Code Playgroud)

如果您使用type(variable).__name__它,我们可以阅读


Ala*_*oni 5

虽然问题很老,但我在自己找到合适的方法时偶然发现了这个问题,我认为它仍然需要澄清,至少对于 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
Run Code Online (Sandbox Code Playgroud)

执行此代码段将产生:

Are o and t instances of the same class? True
Run Code Online (Sandbox Code Playgroud)

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

这种__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
Run Code Online (Sandbox Code Playgroud)


tnu*_*nov 5

小心使用isinstance

isinstance(True, bool)
True
>>> isinstance(True, int)
True
Run Code Online (Sandbox Code Playgroud)

但是输入

type(True) == bool
True
>>> type(True) == int
False
Run Code Online (Sandbox Code Playgroud)


Geo*_*rgy 5

在许多实际情况下,您也可以使用typeusing来代替 using ,它用于定义泛型函数由多个函数组成的函数,对不同类型实现相同的操作)。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
Run Code Online (Sandbox Code Playgroud)

以下是其工作原理的一个小示例:

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")
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

另外,我们可以使用抽象类来同时涵盖多种类型:

from collections.abc import Sequence


@say_type.register
def _(arg: Sequence):
    print(f"{arg} is a sequence!")
Run Code Online (Sandbox Code Playgroud)
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")
Run Code Online (Sandbox Code Playgroud)