Add if...else... switch to doctest?

Duncan Booth duncan.booth at invalid.invalid
Fri Oct 19 05:27:31 EDT 2012


David <zhushenli at gmail.com> wrote:

> Hello, how to add if...else... switch to doctest?
> E.g. function outputs different value when global_var change.
> 
> """
> if (global_var == True):
>>>> function()
> [1,2]
> else:
>>>> function()
> [1,2,3]
> """
> 
> Thank you very much.

One other case the other replies don't seem to have covered:

If the global variable is determined by the environment, outside your 
control, and by implication doesn't change while your program is running, 
then you should use two separate functions:

if sys.platform=='win32':
    def function():
        """
        >>> function()
        [1, 2]
        """
        return [1, 2]

else:
    def function():
        """
        >>> function()
        [1, 2, 3]
        """
        return [1, 2, 3]
    
and if it's more than one such function use separate modules.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list