Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82297
| Date | 2014-12-11 03:43 +1100 |
|---|---|
| Subject | Python 2/3 compatibility code that reads like English! |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16817.1418229819.18130.python-list@python.org> (permalink) |
So you have a string of text, either a Unicode string in Python 3, or
a byte string that's meant to be UTF-8. Most of the way through,
you're working with the native string type, for compatibility with
other sections of code. But then you want to be certain you're working
with a Unicode string...
if str is bytes:
some_string = some_string.decode('UTF-8')
I just love how that condition reads. Who says cross-branch
compatibility code has to be ugly? :)
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Python 2/3 compatibility code that reads like English! Chris Angelico <rosuav@gmail.com> - 2014-12-11 03:43 +1100
csiph-web