python内置方法、模块讲解(三)

  • 2016-02-23 00:10:44
  • 1447
  • 0

八、buffer类

1、定义:

buffer(object[, offset[, size]])
The object argument must be an object that supports the buffer call interface (such as strings, arrays, and buffers). A new buffer object will be created which references the object argument. The buffer object will be a slice from the beginning of object (or from the specified offset). The slice will extend to the end of object (or will have a length given by the size argument).

2、解释:输入参数object必须支持缓存调用接口(如字符串、数组、buffer)。用来创建一个引用object参数的新buffer对象, 这个buffer可以是以object参数的一个切片,切片所取的位置由offset、size参数决定。

3、举例:

>>> s = 'Hello world'
>>> t = buffer(s, 5, 5)
>>> t
<read-only buffer for 0x000000000282FE10, size 5, offset 5 at 0x00000000029BBED8>
>>> print t
 worl
>>> t = buffer(s, 5, 6)
>>> print t
 world

九、bytearray类

1、定义:

class bytearray(object)
 |  bytearray(iterable_of_ints) -> bytearray.
 |  bytearray(string, encoding[, errors]) -> bytearray.
 |  bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray.
 |  bytearray(memory_view) -> bytearray.
 |
 |  Construct an mutable bytearray object from:
 |    - an iterable yielding integers in range(256)
 |    - a text string encoded using the specified encoding
 |    - a bytes or a bytearray object
 |    - any object implementing the buffer API.
 |
 |  bytearray(int) -> bytearray.
 |
 |  Construct a zero-initialized bytearray of the given length.

2、解释:将可迭代的int, 特定编码的文本字符串,字节或者一个bytearray对象, 任何继承buffer API的对象变成bytearray(字节流)对象

3、举例:

>>> bytearray(range(2))
bytearray(b'\x00\x01')
>>> bytearray('ab')
bytearray(b'ab')
>>> bytearray(3)
bytearray(b'\x00\x00\x00')
注:单独输入整型, 会生成相应长度的字节流

十、bytes对象

1、定义:

class str(basestring)
 |  str(object='') -> string
 |
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.

2、解释:从定义看, bytes与str 的功能一样:将一个object转换成string。

3、举例:

>>> bytes(u'a')
'a'
>>> bytes(2)
'2'

十一、callable方法

1、定义:

callable(...)
    callable(object) -> bool

    Return whether the object is callable (i.e., some kind of function).
    Note that classes are callable, as are instances with a __call__() method.

2、解释:判断一个object是否可调用,如可以调用如:方法,以及有__call__()函数的类与对象,返回True, 否则返回False。

3、举例:

>>> callable(help)
True
>>> callable(dir)
True
>>> callable('a')
False
>>> callable(1)
False

 


发表评论

* *