doctest environment question

tag thomas.guest at gmail.com
Tue May 22 04:44:41 EDT 2007


On 22 May, 08:59, Peter Otten <__pete... at web.de> wrote:

[snip]

> inspect.getmodule(f) returns None because f() is not defined in a module.

OK. But there was a module when I ran interactively?

> You can either move f() to a helper module and then
>
> from helper_module import f

Yes.

> or modify anouncement_function() to not rely on that non-existent module
>
> >>> def announce_function(f):
>
> ...     " Rebind f within a module so that calls to f are announced. "
> ...     f.func_globals[f.__name__] = announce(f)
> ...

I think this is what I should be doing. Very nice! You're modifying
f's own gloabl environment.
>
> > Let's give it a try. This next works fine in an interactive Python
> > session but fails when doctested.
>
> >>>> def h(): pass
> > ...
> >>>> announce_function(h)
> >>>> h()
> > Calling h
>
> Even when it works, implicitly modifying global variables is bad style.

I have to admit it didn't feel right, but I didn't come up with the
idea you present. Thanks again for your help.




More information about the Python-list mailing list