Can I Ensure That Users That Import My Python Code Use Unicode Literals?
My code includes from __future__ import unicode_literals and has many functions that accept (and expect) Unicode strings as input in order to function fully. Is there a way to ens
Solution 1:
No, unicode_literals
is a per-module configuration and must be imported in each and every module where it is to be used.
The best way is just to mention it in the docstring of my_func
that you are expecting unicode objects.
If you insist, you could enforce it to fail early:
defmy_func(my_arg):
ifnotisinstance(my_arg, unicode):
raise Exception('This function only handles unicode inputs')
If you need it in many places, it might be nicer to implement it with a decorator.
On python3.5 or up, you could use type hints to enforce this.
Post a Comment for "Can I Ensure That Users That Import My Python Code Use Unicode Literals?"