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)
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
会工作).
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.
Aar*_*all 21
确定对象的类型 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
支持这一点.
更妙的是,你可能会寻找一个特定的抽象基类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)
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)
小智 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__
它,我们可以阅读
虽然问题很老,但我在自己找到合适的方法时偶然发现了这个问题,我认为它仍然需要澄清,至少对于 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)
小心使用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)
在许多实际情况下,您也可以使用type
using来代替 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)