Exhaustive Unit Testing

Terry Reedy tjreedy at udel.edu
Fri Nov 28 13:02:14 EST 2008


bearophileHUGS at lycos.com wrote:
> Terry Reedy:
> 

>> A 'function' only needs to be nested if it is intended to be
>> different (different default or closure) for each execution of its
>> def.<
> 
> Or maybe because you want to denote a logical nesting, or maybe 
> because you want to keep the outer namespace cleaner, etc etc.

I was already aware of those *wants*, but they are not *needs*, in the 
sense I meant.  A single constant function does not *need* to be nested 
and regenerated with each call.

A standard idiom, I think, is give the foo-associated function a private 
foo-derived name such as _foo or _foo_bar.  This keeps the public 
namespace clean and denotes the logical nesting.  I *really* would not 
move things around after testing.

For the attribute approach, you could lobby for the so-far rejected

def foo(x):
   return foo.bar(3*x)

def foo.bar(x):
   return x*x

In the meanwhile...

def foo(x):
   return foo.bar(3*x)

def _(x):
   return x*x
foo.bar = _

Or write a decorator so you can write

@funcattr(foo. 'bar')
def _

(I don't see any way a function can delete a name in the namespace that 
is going to become the class namespace, so that
@fucattr(foo)
def bar...
would work.)

Terry Jan Reedy




More information about the Python-list mailing list