Exhaustive Unit Testing

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Nov 27 20:00:51 EST 2008


On Thu, 27 Nov 2008 10:45:47 -0800, bearophileHUGS wrote:

> A question for other people: Can Python change a little to allow nested
> functions to be tested? I think this may solve some of my problems.

Remember that nested functions don't actually exist as functions until 
the outer function is called, and when the outer function is called they 
go out of scope and cease to exist. For this to change wouldn't be a 
little change, it would be a large change.

I can see benefit to the idea. Unit testing, as you say. I also like the 
idea of doing this:

def foo(x):
    def bar(y):
        return y+1
    return x**2+bar(x)

a = foo.bar(7)


However you can get the same result (and arguably this is the Right Way 
to do it) with a class:

class foo:
    def bar(y):
        return y+1
    def __call__(self, x):
        return x**2 + self.bar(x)
foo = foo()


Unfortunately, this second way can't take advantage of nested scopes in 
the same way that functions can.


-- 
Steven



More information about the Python-list mailing list