一、创造对象的函数
1.basestring
()
str和unicode的父对象,不能被实例化,常被用来判断对象是否是str和unicode的实例。
>>> obj = 'abc'
>>> isinstance(obj, basestring)
True
>>> obj = u'中'
>>> isinstance(obj, basestring)
True
>>> isinstance(object, basestring)
False
2.bin(x)
将整数转为二进制字符串。
>>> bin(1)
'0b1'
>>> bin(10)
'0b1010'
>>> bin(-1)
'-0b1'
>>> bin(-178)
'-0b10110010'
>>> bin(0)
'0b0'
3.class bool
([x])
bool本身是int的子类,不能被继承,且仅有True和False两个实例。
>>> bool()
False
>>> bool(None)
False
>>> bool(0)
False
>>> bool(1)
True
>>> bool(object())
True
4.class bytearray
([source[, encoding[, errors]]])
返回一个byte数组。Bytearray类型是一个可变的序列,并且序列中的元素的取值范围为 [0 ,255]。
>>> a = bytearray(10)
>>> a
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> a = bytearray('abc')
>>> a
bytearray(b'abc')
>>> a[0]
97
>>> bytearray(xrange(5))
bytearray(b'\x00\x01\x02\x03\x04')
5.class complex
([real[, imag]])
返回一个复数对象,参数可以是两个整数,也可以是一个字符串(字符串中间不能有空格分开)。
>>> complex('1+2j')
(1+2j)
>>> complex(1,2)
(1+2j)
>>> complex(2)
(2+0j)
>>> complex()
0j
>>> complex('2 + 3j')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: complex() arg is a malformed string