[Python-Dev] Merging PEP 310 and PEP 340-redux?

Fredrik Lundh fredrik at pythonware.com
Fri May 13 18:42:56 CEST 2005


Michael Hudson wrote:

> Looking at my above code, no (even though I think I've rendered the
> point moot...).  Compare and contrast:
>
> @template
> def redirected_stdout(out):
>     save_stdout = sys.stdout
>     sys.stdout = out
>
>     yield None
>
>     sys.stdout = save_stdout
>
> class redirected_stdout(object):
>
>     def __init__(self, output):
>         self.output = output
>
>     def __enter__(self):
>         self.save_stdout = sys.stdout
>         sys.stdout = self.output
>
>     def __exit__(self):
>         sys.stdout = self.save_stdout
>
> The former is shorter and contains less (well, no) 'self.'s, but I
> think I find the latter somewhat clearer.

the same argument could be used (and was probably used) against
generators: why not just use __getitem__ and instance state?

as soon as you write something longer than four lines, using more
than one state variable, you'll find that generator-based code is a
lot more readable.

</F>





More information about the Python-Dev mailing list