I'm trying to adapt some old code to make it usable with both Python 2 and 3. I'm using the six package for this task.
If I have u'abc'
in 2.7, I can use the six.u() function and replace it with six.u('abc')
to make it work in both 2.7 and 3.x.
How do I do something similar for:
unicode(value, errors='ignore', encoding='utf-8')
There is no unicode
function in 3.x and I can't just replace it with str
because that will change the meaning in 2.7.
if isinstance(value, basestring): # do something
There is no basestring
in 3.x and again I can't just replace it with str
without changing the meaning.
Of course, I can use the py2/3 checks with six.PY2
or six.PY3
to run one of two versions but is there a better way?