views:

81

answers:

2

Is there any way to write unittests or doctests for innerfunc?

def outerfunc():
    def innerfunc():
        do_something()
    return innerfunc()
+5  A: 

Only if you provide a way to extract the inner function object itself, e.g.

def outerfunc(calltheinner=True):
    def innerfunc():
        do_something()
    if calltheinner:
        return innerfunc()
    else:
        return innerfunc

If your outer function insists on hiding the inner one entirely inside itself (never letting it percolate outside when properly cajoled to do so), your unit-tests are powerless to defeat this strong bid for extreme and total privacy;-).

Alex Martelli
that was my suspicion, thank you.
Gattster
A: 

This is actually an old open Python issue:

There's a candidate patch (from 2007) that makes doctest find nested functions, but someone probably needs to push this.

Piet Delport