Online Book Reader

Home Category

Learning Python - Mark Lutz [495]

By Root 1535 0

>>> C[1] = b'Y'[0]

>>> C

bytearray(b'xYam')

Processing bytearray objects borrows from both strings and lists, since they are mutable byte strings. Besides named methods, the __iadd__ and __setitem__ methods in bytearray implement += in-place concatenation and index assignment, respectively:

# Methods overlap with both str and bytes, but also has list's mutable methods

>>> set(dir(b'abc')) - set(dir(bytearray(b'abc')))

{'__getnewargs__'}

>>> set(dir(bytearray(b'abc'))) - set(dir(b'abc'))

{'insert', '__alloc__', 'reverse', 'extend', '__delitem__', 'pop', '__setitem__'

, '__iadd__', 'remove', 'append', '__imul__'}

You can change a bytearray in-place with both index assignment, as you’ve just seen, and list-like methods like those shown here (to change text in-place in 2.6, you would need to convert to and then from a list, with list(str) and ''.join(list)):

# Mutable method calls

>>> C

bytearray(b'xYam')

>>> C.append(b'LMN') # 2.6 requires string of size 1

TypeError: an integer is required

>>> C.append(ord('L'))

>>> C

bytearray(b'xYamL')

>>> C.extend(b'MNO')

>>> C

bytearray(b'xYamLMNO')

All the usual sequence operations and string methods work on bytearrays, as you would expect (notice that like bytes objects, their expressions and methods expect bytes arguments, not str arguments):

# Sequence operations and string methods

>>> C + b'!#'

bytearray(b'xYamLMNO!#')

>>> C[0]

120

>>> C[1:]

bytearray(b'YamLMNO')

>>> len(C)

8

>>> C

bytearray(b'xYamLMNO')

>>> C.replace('xY', 'sp') # This works in 2.6

TypeError: Type str doesn't support the buffer API

>>> C.replace(b'xY', b'sp')

bytearray(b'spamLMNO')

>>> C

bytearray(b'xYamLMNO')

>>> C * 4

bytearray(b'xYamLMNOxYamLMNOxYamLMNOxYamLMNO')

Finally, by way of summary, the following examples demonstrate how bytes and bytearray objects are sequences of ints, and str objects are sequences of characters:

# Binary versus text

>>> B # B is same as S in 2.6

b'spam'

>>> list(B)

[115, 112, 97, 109]

>>> C

bytearray(b'xYamLMNO')

>>> list(C)

[120, 89, 97, 109, 76, 77, 78, 79]

>>> S

'spam'

>>> list(S)

['s', 'p', 'a', 'm']

Although all three Python 3.0 string types can contain character values and support many of the same operations, again, you should always:

Use str for textual data.

Use bytes for binary data.

Use bytearray for binary data you wish to change in-place.

Related tools such as files, the next section’s topic, often make the choice for you.

Using Text and Binary Files

This section expands on the impact of Python 3.0’s string model on the file processing basics introduced earlier in the book. As mentioned earlier, the mode in which you open a file is crucial—it determines which object type you will use to represent the file’s content in your script. Text mode implies str objects, and binary mode implies bytes objects:

Text-mode files interpret file contents according to a Unicode encoding—either the default for your platform, or one whose name you pass in. By passing in an encoding name to open, you can force conversions for various types of Unicode files. Text-mode files also perform universal line-end translations: by default, all line-end forms map to the single '\n' character in your script, regardless of the platform on which you run it. As described earlier, text files also handle reading and writing the byte order mark (BOM) stored at the start-of-file in some Unicode encoding schemes.

Binary-mode files instead return file content to you raw, as a sequence of integers representing byte values, with no encoding or decoding and no line-end translations.

The second argument to open determines whether you want text or binary processing, just as it does in 2.X Python—adding a “b” to this string implies binary mode (e.g., "rb" to read binary data files). The default mode is "rt"; this is the same as "r", which means text input (just as in 2.X).

In 3.0, though, this mode argument to open also implies an object type for file content representation, regardless of

Return Main Page Previous Page Next Page

®Online Book Reader