sharing data across Examples docstrings

Martin Di Paola martinp.dipaola at gmail.com
Fri Jan 14 21:53:38 EST 2022


Hello,

I understand that you want to share data across examples (docstrings) 
because you are running doctest to validate them (and test).

The doctest implementation evaluates each docstring separately without 
sharing the context so the short answer is "no".

This is a limitation of doctest but it is not the only testing engine 
that you can use.

You could use "byexample" ( https://byexamples.github.io ) which it 
shares the context by default.

byexample has more features and fixes other caveats of doctests, but 
don't take me too serious, I have a natural bias because I'm its author.

If you want to go with byexample, you may want to try its "doctest 
compatibility mode" first so you don't have to rewrite any test.
( https://byexamples.github.io/byexample/recipes/python-doctest )

Let me know if it is useful for you.

Thanks,
Martin.

On Tue, Jan 11, 2022 at 04:09:57PM -0600, Sebastian Luque wrote:
>Hello,
>
>I am searching for a mechanism for sharing data across Examples sections
>in docstrings within a class.  For instance:
>
>class Foo:
>
>    def foo(self):
>        """Method foo title
>
>        The example generating data below may be much more laborious.
>
>        Examples
>        --------
>        >>> x = list("abc")  # may be very long and tedious to generate
>
>        """
>        pass
>
>    def bar(self):
>        """Method bar title
>
>        Examples
>        --------
>        >>> # do something else with x from foo Example
>
>        """
>        pass
>
>
>Thanks,
>-- 
>Seb
>-- 
>https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list