unicode_literals and type()

前端 未结 1 1793
暗喜
暗喜 2021-02-19 22:43

I\'m having problems supporting python2 and python3 on a type() call. This demonstrates the problem:

from __future__ import unicode_literals

name=\         


        
1条回答
  •  青春惊慌失措
    2021-02-19 23:16

    six.b is written under the assumption that you won't use unicode_literals (and that you'll pass a string literal to it, as the documentation states), so the Python 2 implementation is just def b(s): return s as a Python 2 string literal is already a byte string.

    Either don't use unicode_literals in this module, or use (as a comment suggests) str(name). In Python 3, that is a no-op. In Python 2, it silently converts the unicode string to a byte string (assuming some encoding that I can't be bothered to remember, but it's a superset of ASCII so you should be fine).

    0 讨论(0)
提交回复
热议问题