Top level of a recursive function

Rob Cliffe rob.cliffe at btinternet.com
Wed Dec 14 14:17:50 EST 2022



On 14/12/2022 13:49, Stefan Ram wrote:
> I also found an example similar to what was discussed here
>    in pypy's library file "...\Lib\_tkinter\__init__.py":
>
> |def _flatten(item):
> |    def _flatten1(output, item, depth):
> |        if depth > 1000:
> |            raise ValueError("nesting too deep in _flatten")
> |        if not isinstance(item, (list, tuple)):
> |            raise TypeError("argument must be sequence")
> |        # copy items to output tuple
> |        for o in item:
> |            if isinstance(o, (list, tuple)):
> |                _flatten1(output, o, depth + 1)
> |            elif o is not None:
> |                output.append(o)
> |
> |    result = []
> |    _flatten1(result, item, 0)
> |    return tuple(result)
>
>    .
>
>
This presumably results in an (avoidable) run-time overhead from 
constructing _flatten1 every time _flatten is called (and having it 
garbage-collected later).
Best wishes
Rob Cliffe


More information about the Python-list mailing list