Changes between Version 46 and Version 47 of DeveloperGuidelines/Py_2_3


Ignore:
Timestamp:
07/04/19 19:55:12 (5 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/Py_2_3

    v46 v47  
    199199=== Don't unicode.encode ===
    200200
    201 Since there is no difference between {{{unicode}}} and {{{str}}}, using the {{{encode()}}} method will produce {{{bytes}}} rather than {{{str}}}.
    202 
    203 {{{bytes}}} differs from string in that it is an array of integers rather than an array of characters. It will also give a distorted result with any later {{{str()}}} or {{{s3_str()}}}.
    204 
    205 If you just want to encode a potential {{{unicode}}} instance as an utf-8 encoded {{{str}}}, use {{{s3_str}}} rather than {{{unicode.encode}}}.
     201Since there is no difference between {{{unicode}}} and {{{str}}}, using the {{{encode()}}} method will produce {{{bytes}}} rather than {{{str}}}. A {{{bytes}}} object differs from string in that it is an array of integers rather than an array of characters. It will also give a distorted result with any later {{{str()}}} or {{{s3_str()}}}.
     202
     203{{{#!python
     204if isinstance(x, unicodeT):  # unicodeT maps to str in Py3
     205    x = x.encode("utf-8")    # x becomes a bytes-object in Py3, unlike in Py2 where it becomes a str
     206
     207str(x)                       # thus, in Py3, this results in something like "b'example'" instead of the expected "example"
     208
     209x[1]                         # is "e" in Py2, but 120 (an integer!) in Py3
     210}}}
     211
     212If you just want to encode a potential {{{unicode}}} instance as an utf-8 encoded {{{str}}}, use {{{s3_str}}} rather than {{{unicode.encode}}}:
     213{{{#!python
     214# Do this instead (without type-check):
     215x = s3_str(x)
     216}}}