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}}}. |
| 201 | Since 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 |
| 204 | if 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 | |
| 207 | str(x) # thus, in Py3, this results in something like "b'example'" instead of the expected "example" |
| 208 | |
| 209 | x[1] # is "e" in Py2, but 120 (an integer!) in Py3 |
| 210 | }}} |
| 211 | |
| 212 | If 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): |
| 215 | x = s3_str(x) |
| 216 | }}} |