Differences between revisions 2 and 3
Revision 2 as of 2007-08-10 23:23:48
Size: 989
Editor: neu67-4-88-160-66-91
Comment:
Revision 3 as of 2007-08-10 23:35:30
Size: 1258
Editor: neu67-4-88-160-66-91
Comment:
Deletions are marked like this. Additions are marked like this.
Line 26: Line 26:

== open issues ==

=== hash(bytes) ===

bytes is mutable and so it's not hashable. Hacks/Workaorounds:
 * use buffer(value)
 * use str8(value)

Other solutions:
 * create frozenbytes type
 * avoid using hash

Hash is used when bytes is a dictionary key.

Python 2.x

Python 2.x has two types to store a string:

  • str: bytes string procced as character string which is a mistake
  • unicode: character string (unicode)

Both classes has same methods and are very similar.

Python 3000

Python 3000 use two very different types:

  • bytes: bytes string which can be see as a list of [0..255] integers
  • str: character string (unicode), exactly the same type than Python 2.x "unicode"

old str and new bytes

Differences between Python 2.x "str" and Python 3000 "bytes":

  • str is immutable, bytes is mutable
  • bytes "lacks" many methods: strip, lstrip, rstrip, lower, upper, etc.

choose between bytes and str

When you migration from Python 2.x to Python 3000, you have to ask youself: do I manipulate characters or bytes (integers)? "A" is a character and 65 is an integer. Examples:

  • a network socket manipulate bytes
  • a text parser manipulates characters (use lower, strip, etc. methods)

open issues

hash(bytes)

bytes is mutable and so it's not hashable. Hacks/Workaorounds:

  • use buffer(value)
  • use str8(value)

Other solutions:

  • create frozenbytes type
  • avoid using hash

Hash is used when bytes is a dictionary key.

BytesStr (last edited 2019-10-19 22:00:22 by FrancesHocutt)

Unable to edit the page? See the FrontPage for instructions.