ast wrote at 2020-5-4 15:20 +0200:
>doctest of the sample function funct() doesn't works
>because flag used by funct() is the one defined in
>first line "flag = True" and not the one in the
>doctest code ">>> flag = False".
>
>Any work around known ?
>
>
>flag = True              # <- funct() always use this one
>
>def funct():
>     """
>     Code for doctest:
>
>     >>> flag = True
>     >>> funct()
>     'Yes'
>     >>> flag = False     #  <- Ignored unfortunalely
>     >>> funct()
>     'No'
>     """
>
>     if flag:
>         return "Yes"
>     else:
>         return "No"
>
>if __name__ == "__main__":
>     import doctest
>     doctest.testmod()
>
>
>
>Failed example:
>     funct()
>Expected:
>     'No'
>Got:
>     'Yes'
>**********************************************************************
>1 items had failures:
>    1 of   4 in __main__.funct
>***Test Failed*** 1 failures.

The "flag" used inside "funct" refers to the definition
in "funct.__globals__" (i.e. the module where "funct" is defined in).

As your observation shows, this is not the "flag" changed by the
doctest. The doctest behavior reflects the use in a function or a
foreign module (rather than a use at the top level of the module
defining "funct").
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to