[Python-Dev] PEP 414 updated

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Sun Mar 4 13:25:06 CET 2012

On 03/04/2012 10:34 AM, Vinay Sajip wrote:
> https://bitbucket.org/vinay.sajip/uprefix/

>>>> import uprefix; uprefix.register_hook()
>>>> import frob.subwob.subsubwob
>>>> frob.subwob.subsubwob.w


it's pretty cool that 150 lines is enough to have this functionality.

This guard:

  if sys.version_info[0] < 3:
      raise NotImplementedError('This hook is implemented for Python 3 only')

Wouldn't it be better if the hook did nothing when on python 2?
I think it'll make it necessary to use something like

  import sys
  if sys.version_info[0] < 3:
      import uprefix

in the calling code to enable the code to run unchanged on both branches.

Also: have you though about providing a context manager which does
register_hook() in __enter__() and unregister_hook() in __exit__()?

I think that some code will want to enable the hook only for specific
modules. The number of lines could be minimized with something like this:
  import uprefix
  with uprefix.hook:
     import abcde_with_u
     import bcdef_with_u
  import other_module_without_u


More information about the Python-Dev mailing list