<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Interesting idea, though doing this with present Python does not seem to<br>
be very painful:<br>
<br>
    class cd(object):<br>
<br>
        def __init__(self, path):<br>
            self.inner_path = path<br>
<br>
        def __enter__(self):<br>
            self.outer_path = os.getcwd()<br>
            os.chdir(self.inner_path)<br>
            return self<br>
<br>
        def __exit__(self, exc_type, exc_val, exc_tb):<br>
            os.chdir(self.outer_path)<br>
<br>
    def my_generator(path):<br>
        with cd(path) as context:<br>
            output = do_something()<br>
            with cd(context.outer_path):<br>
                yield output<br>
            ...<br></blockquote><div><br>Yes, that's possible, although as the context manager gets more complicated (e.g. modifying os.environ as well as working directory, I'd currently start using something like this:<br>


<br>def my_generator(arg):<br>    with context_manager(arg) as context:<br>        output = do_something()<br>        with context.undo():<br>            yield output<br>        ...<br><br>But nevertheless adding __yield__ and __send__ (or equivalent) to context managers means that the author of the context manager can make sure that it's free of unintended side effects, rather than relying on the user to be careful as in the examples above.<br>


<br><div>Cheers,</div><div><br></div><div>J. D. Bartlett</div><br></div></div>