Python Forum
byte string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: byte string (/thread-7991.html)



byte string - Skaperen - Feb-02-2018

i can do x=b'foo' then [y for y in x] gives me [102, 111, 111]. how can i reverse that so [98, 97, 114] -> b'bar'?

i found my own answer in Python3: bytes(bytearray([98, 97, 114])) -> b'bar'. there can be no answer in Python2 because b'anything' cannot exist in Python2.


RE: byte string - Gribouillis - Feb-02-2018

(Feb-02-2018, 03:03 AM)Skaperen Wrote: there can be no answer in Python2 because b'anything' cannot exist in Python2.
In python2, b'foo' exists, and it is equal to 'foo'.
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'anything'
>>> x = 'anything'
>>> a = bytearray(x)
>>> list(a)
[97, 110, 121, 116, 104, 105, 110, 103]
>>> str(a)
'anything'
>>> L = list(a)
>>> str(bytearray(L))
'anything'



RE: byte string - Skaperen - Feb-03-2018

try doing that without bytearray.

i'd argue that b'foo' (a literal) becomes a regular character string because bytes==str is True. does this work in 2.0? does 2.0 matter any more?

or can you make any code which produces an actual bytes (not bytearray) type?

i've been doing stuff in Python3. but i still like to make my code work (and work right) in Python2. that is because many systems still come with Python2 but not Python3 ready to use (even though installing or upgrading to Python3 is an available option for many).


RE: byte string - snippsat - Feb-03-2018

(Feb-03-2018, 03:34 AM)Skaperen Wrote: or can you make any code which produces an actual bytes (not bytearray) type?
No the new bytes type is 3.x only.
As Grib show so is the 2.6 --> 2.7 bytes built-in just an alias to the str type.
But bytes in 2.6 --> 2.7is not the same as bytes in Python 3.
The Doc Byte Literals
Quote: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.



RE: byte string - Skaperen - Feb-04-2018

2.x lets you refer to name "bytes" which is the same as name "str". 2.x lets you express literal strings with a "b" prefix which have the same value as the same string without the "b" prefix. does this mean "2.x has bytes" or not? even though if bytes==str: is my quick test to see if my code is running in 2.x before i import sys, i say "not".

but i was basically saying (expressed with more detail this time) "you cannot, in 2.x, create and reference a data type that is an immutable type bytes, but sure you can do so a data type that is a mutable equivalent. the literal b'anything' certainly does not do this in 2.x ".


RE: byte string - Gribouillis - Feb-04-2018

Here is what python 2.7 says:
>>> bytes is str
True