Local Features
Is there a way to inherit locally defined functions? As an example, I would like
def foo(): """ >>> foo() testfoo""" def foo2(): """ >>> 1/0 """ print 'testfoo' foo2() DO NOT pass the test. But still, I would not want foo2 to be global for the whole module ...
Thanks. I already feared that there would be no way to get around the code outside of docstring. However, I thought there might be a trick for importing function locales and thus gaining access to nested functions. Anyway, a solution using Alex's approach will read
def foo(debug=False): """ >>> foo() testfoo >>> foo(debug=True) """ def foo2(): """ >>> 1/0""" print 'testfoo' if debug : import doctest for f in [foo2]: doctest.run_docstring_examples(f,locals()) foo2() Now the only question is how to automate this approach, so there is something like
for f in locals().values(): doctest.run_docstring_examples(f,locals()) but without imported and built-in functions and variables.
You just have a problem with spaces - if you fix it, for example, as follows:
def foo(): """ >>> foo() testfoo""" def foo2(): """ >>> 1/0 """ print 'testfoo' foo2() if __name__ == '__main__': import doctest doctest.testmod() the test passes just fine.