Shadowing builtins (was Re: a pyrex-inspired for-i-from-1-to-n...)

Skip Montanaro skip at pobox.com
Wed Apr 2 15:42:13 EST 2003


    >> range = my_custom_range_function
    >> 
    >> would be OK. While,
    >> 
    >> some_random_module.range = my_custom_range_function
    >> 
    >> would not.

    Peter> Hmm.... that is exactly what is needed to make this effective in
    Peter> unit testing.  One imports the module under test, then fakes the
    Peter> open method in that other module:
    ...
    Peter> I'm not sure what other approach could be as simple, elegant, and
    Peter> effective and I worry about losing it.  Surely there are many
    Peter> other people doing similar things who would be hurt by this.

How about calling new.module() to create a new module object from the one
you want to test which has suitably munged globals?

Skip





More information about the Python-list mailing list