基本上我想这样做:
obj = 'str'
type ( obj ) == string
我试着:
type ( obj ) == type ( string )
但这并没有起作用。
还有,其他类型的呢?例如,我无法复制NoneType。
基本上我想这样做:
obj = 'str'
type ( obj ) == string
我试着:
type ( obj ) == type ( string )
但这并没有起作用。
还有,其他类型的呢?例如,我无法复制NoneType。
当前回答
要获取类型,使用__class__成员,如unknown_thing.__class__
Talk of duck-typing is useless here because it doesn't answer a perfectly good question. In my application code I never need to know the type of something, but it's still useful to have a way to learn an object's type. Sometimes I need to get the actual class to validate a unit test. Duck typing gets in the way there because all possible objects have the same API, but only one is correct. Also, sometimes I'm maintaining somebody else's code, and I have no idea what kind of object I've been passed. This is my biggest problem with dynamically typed languages like Python. Version 1 is very easy and quick to develop. Version 2 is a pain in the buns, especially if you didn't write version 1. So sometimes, when I'm working with a function I didn't write, I need to know the type of a parameter, just so I know what methods I can call on it.
这就是__class__参数派上用场的地方。这(据我所知)是获取对象类型的最佳方法(可能是唯一的方法)。
其他回答
你很接近了!字符串是模块,不是类型。你可能想比较obj的类型和字符串的类型对象,即str:
type(obj) == str # this works because str is already a type
另外:
type(obj) == type('')
注意,在python2中,如果obj是unicode类型,那么上述两种方法都不能工作。isinstance()也不会。看看约翰对这篇文章的评论,了解如何解决这个问题……我已经试着记住它大约10分钟了,但是有一个记忆块!
因为type返回一个对象,所以可以使用object.name访问对象的名称
例子:
years = 5
user = {'name':'Smith', 'age':20}
print(type(a).__name__)
输出:“int”
print(type(b).__name__ )
输出:“东西”
要获取类型,使用__class__成员,如unknown_thing.__class__
Talk of duck-typing is useless here because it doesn't answer a perfectly good question. In my application code I never need to know the type of something, but it's still useful to have a way to learn an object's type. Sometimes I need to get the actual class to validate a unit test. Duck typing gets in the way there because all possible objects have the same API, but only one is correct. Also, sometimes I'm maintaining somebody else's code, and I have no idea what kind of object I've been passed. This is my biggest problem with dynamically typed languages like Python. Version 1 is very easy and quick to develop. Version 2 is a pain in the buns, especially if you didn't write version 1. So sometimes, when I'm working with a function I didn't write, I need to know the type of a parameter, just so I know what methods I can call on it.
这就是__class__参数派上用场的地方。这(据我所知)是获取对象类型的最佳方法(可能是唯一的方法)。
首先,避免所有类型比较。它们很少是必要的。有时,它们有助于检查函数中的参数类型——即使这种情况也很少见。错误类型的数据将引发异常,而这正是您所需要的。
所有基本转换函数都将映射为类型函数。
type(9) is int
type(2.5) is float
type('x') is str
type(u'x') is unicode
type(2+3j) is complex
还有其他一些情况。
isinstance( 'x', basestring )
isinstance( u'u', basestring )
isinstance( 9, int )
isinstance( 2.5, float )
isinstance( (2+3j), complex )
顺便说一句,None从来不需要任何这种类型的检查。None是唯一的实例。None对象是一个单例对象。只检查None
variable is None
顺便说一下,一般情况下不要使用上述方法。使用普通异常和Python本身的自然多态。
isinstance工作原理:
if isinstance(obj, MyClass): do_foo(obj)
但是,请记住:如果它看起来像鸭子,如果它听起来像鸭子,它就是鸭子。
编辑:对于None类型,你可以简单地做:
if obj is None: obj = MyClass()