Can I ensure that users that import my Python code

2019-08-16 01:42发布

问题:

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 ensure that users (in scripts, Python, or IPython, etc.) also use Unicode literals so that, for example

my_func("AβC")

does not cause an error ("ascii' codec can't decode byte 0xce ...") and so that

my_func(u"AβC")

is not necessary?

回答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:

def my_func(my_arg):
    if not isinstance(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.