1. 自定义类型布尔值
自定义的类如果定义了__nonzero__()
或者__len__()
且值为0,那么它们的布尔值就是Flase
。
2. 对象比较
a is b
# equivalent to
id(a) == id(b)
3. 标准类型内建函数
- type()
- cmp()
- str()
- repr()
- isinstance()
3.1 type()
得到一个对象的类型,并返回相应的type对象。
>>> type(1)
<type 'int'>
>>> type('Furzoom')
<type 'str'>
>>> type(type(1))
<type 'type'>
>>>
3.2 cmp()
对于自定义类型,cmp()会调用__cmp__()
函数。
>>> a,b=1,2
>>> cmp(a,b)
-1
>>> cmp(b,a)
1
>>> b=1
>>> cmp(a,b)
0
>>> a,b='abc','xyz'
>>> cmp(a,b)
-1
>>> cmp(b,a)
1
>>> b='abc'
>>> cmp(a,b)
0
>>>
3.3 str() and repr()
通常情况下obj = eval(repr(obj))
。
repr()
与反引号运算符得到的结果是一样的。
3.4 isinstance()
返回对应是否是某种类型。
>>> isinstance(3, int)
True
4. 类型工厂函数
- int(), long(), float(), double()
- str(), unicode(), basestring()
- list(), tuple()
- type()
- dict()
- bool()
- set(), frozenset()
- object()
- classmethod()
- staticmethod()
- super()
- property()
- file()