Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the bytes type in python 2.7 and PEP-358

Tags:

python

types

According to PEP 358, a bytes object is used to store a mutable sequence of bytes (0-255), raising if this is not the case.

However, my python 2.7 says otherwise

>>> bytes([1,2,3]) '[1, 2, 3]' >>> bytes([280]) '[280]' >>> bytes is str True >>> bytes <type 'str'> 

Does anyone have a clue on the reason why the PEP is declared Final, but the implementation does not conform ?

like image 322
Stefano Borini Avatar asked May 05 '11 17:05

Stefano Borini


People also ask

What is the bytes type in Python?

In short, the bytes type is a sequence of bytes that have been encoded and are ready to be stored in memory/disk. There are many types of encodings (utf-8, utf-16, windows-1255), which all handle the bytes differently. The bytes object can be decoded into a str type. The str type is a sequence of unicode characters.

What is bytes and Bytearray in Python?

The bytes() function returns a bytes object. It can convert objects into bytes objects, or create empty bytes object of the specified size. The difference between bytes() and bytearray() is that bytes() returns an object that cannot be modified, and bytearray() returns an object that can be modified.

How do you calculate bytes in Python?

Use the len() function to get the length of a bytes object, e.g. len(my_bytes) . The len() function returns the length (the number of items) of an object and can be passed a sequence (a bytes, string, list, tuple or range) or a collection (a dictionary, set, or frozen set).

How many bits is a byte Python?

Getting the size of an integer Note that 1 byte equals 8 bits. Therefore, you can think that Python uses 24 bytes as an overhead for storing an integer object. It returns 28 bytes. Since 24 bytes is an overhead, Python uses 4 bytes to represent the number 100.


2 Answers

The new bytes type is 3.x only. The 2.x bytes built-in is just an alias to the str type. There is no new type called bytes in 2.x; Just a new alias and literal syntax for str.

Here's the documentation snippet everybody loves:

Python 2.6 adds bytes as a synonym for the str type, and it also supports the b'' notation.

The 2.6 str differs from 3.0’s bytes type in various ways; most notably, the constructor is completely different. In 3.0, bytes([65, 66, 67]) is 3 elements long, containing the bytes representing ABC; in 2.6, bytes([65, 66, 67]) returns the 12-byte string representing the str() of the list.

The primary use of bytes in 2.6 will be to write tests of object type such as isinstance(x, bytes). This will help the 2to3 converter, which can’t tell whether 2.x code intends strings to contain either characters or 8-bit bytes; you can now use either bytes or str to represent your intention exactly, and the resulting code will also be correct in Python 3.0.

like image 67
nosklo Avatar answered Sep 19 '22 03:09

nosklo


The bytes type was introduced in Python 3, but what's being discussed in the PEP is a mutable sequence (bytes is immutable) which was introduced in Python 2.6 under the name bytearray.

The PEP clearly wasn't implemented as stated (and it does say that it was partially superseded by PEP 3137) but I think it's only a question of things being renamed, not features missing. In Python 2 bytes is just an alias for str to aid forward compatibility and so is a red-herring here.

Example bytearray usage:

>>> a = bytearray([1,2,3]) >>> a[0] = 5 >>> a bytearray(b'\x05\x02\x03') 
like image 27
Scott Griffiths Avatar answered Sep 22 '22 03:09

Scott Griffiths