Shadowing builtins (was Re: a pyrex-inspired for-i-from-1-to-n...)
Peter Hansen
peter at engcorp.com
Wed Apr 2 15:32:32 EST 2003
Tim Hochberg wrote:
>
> I believe what was discussed was making shadowing of builtins from
> outside of a module forbidden. So:
>
> range = my_custom_range_function
>
> would be OK. While,
>
> some_random_module.range = my_custom_range_function
>
> would not.
Hmm.... that is exactly what is needed to make this effective in
unit testing. One imports the module under test, then fakes the
open method in that other module:
import unittest
import module_under_test as mut
from mock_files import mock_open
class MyCase(unittest.TestCase):
def setUp(self):
mut.open = mock_open # shadow builtin
def tearDown(self):
del mut.open # remove shadow
def test(self):
mut.doSomething()
I'm not sure what other approach could be as simple, elegant, and
effective and I worry about losing it. Surely there are many other
people doing similar things who would be hurt by this.
-Peter
More information about the Python-list
mailing list