Portable code: __import__ demands different string types between 2 and 3

Ethan Furman ethan at stoneleaf.us
Mon Dec 15 12:23:08 EST 2014


On 12/14/2014 11:29 PM, Ben Finney wrote:
> 
> This entails, I believe, the admonition to ensure text literals are
> Unicode by default::
> 
>     from __future__ import unicode_literals
> 
> and to specify bytes literals explicitly with ‘b'wibble'’ if needed.

For some scripts this works great (I have some), and for some it does not (I have some of those, too).

> The ‘__import__’ built-in function, though, is tripping up.

One work-around I have used is:

  if isinstance(some_var, bytes):
      some_var = some_var.decode('ascii')
  # at this point some_var is unicode in both Pythons

--
~Ethan~

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20141215/ec9db5bf/attachment.sig>


More information about the Python-list mailing list