<div dir="ltr">sorry, it’s a bit more difficult. this works: <a href="https://gist.github.com/flying-sheep/86dfcc1bdd71a33fa3483b83e254084c">https://gist.github.com/flying-sheep/86dfcc1bdd71a33fa3483b83e254084c</a></div><br><div class="gmail_quote"><div dir="ltr">Philipp A. <<a href="mailto:flying-sheep@web.de">flying-sheep@web.de</a>> schrieb am Do., 7. Sep. 2017 um 21:18 Uhr:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><font color="#212121">Sadly it’s hard to create a context manager that skips its body like this:</font></div><span style="color:rgb(33,33,33)"><div><span style="color:rgb(33,33,33)"><br></span></div><font face="monospace">with unpack(computation()) as result:</font></span><font face="monospace"><br style="color:rgb(33,33,33)"><span style="color:rgb(33,33,33)">    do_something_with_result(resu</span><span style="color:rgb(33,33,33)">lt)</span></font><br style="color:rgb(33,33,33)"><div><span style="color:rgb(33,33,33)"><br></span></div><div><span style="color:rgb(33,33,33)">You can do it with some hackery like described here: </span><font color="#212121"><a href="https://stackoverflow.com/a/12594789/247482" target="_blank">https://stackoverflow.com/a/12594789/247482</a></font></div><div><br></div><div><div><font face="monospace">class <span style="color:rgb(33,33,33)">unpack</span>:</font></div><div><font face="monospace">    def __init__(self, pred):</font></div><div><font face="monospace">        self.pred = pred</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    def __enter__(self):</font></div><div><font face="monospace">        if self.pred:</font></div><font face="monospace">            return self.pred</font></div><div><font face="monospace">        # else skip the with block’s body<br></font><div><font face="monospace">        sys.settrace(lambda *args, **kw: None)</font></div><div><font face="monospace">        frame = inspect.currentframe(1)</font></div><div><font face="monospace">        frame.f_trace = self.trace</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    def trace(self, frame, event, arg):</font></div><div><font face="monospace">        raise</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    def __exit__(self, type, value, traceback):</font></div><div><font face="monospace">        return True  # suppress the exception</font></div></div></div><div dir="ltr"><br><div class="gmail_quote"><div dir="ltr">Steven D'Aprano <<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>> schrieb am Do., 7. Sep. 2017 um 18:26 Uhr:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Thu, Sep 07, 2017 at 04:36:40PM +0200, Jason H wrote:<br>
<br>
> I also often wonder why we are left doing an assignment and test. You have two options:<br>
> 1. assign to a variable then test and use<br>
> 2. repeat the function call<br>
<br>
Personally, I don't see what's wrong with the "assign then test" idiom.<br>
<br>
x = something()<br>
if x:<br>
    do_stuff()<br>
<br>
<br>
> I would offer that 'with' [sh|c]ould be used:<br>
> with test() as x:<br>
>    handle_truthy(x)<br>
> else:<br>
>    handle_falsey() # do we provide x here too? Because None vs False?<br>
<br>
<br>
This would cause confusing errors and mysterious behaviour, depending on<br>
whether the test() object was a context manager or not. Which should<br>
take priority? If you see:<br>
<br>
with spam() as x:<br>
   do_stuff<br>
<br>
is that a context manager with block (like "with open(...) as f") or<br>
your boolean if test in disguise?<br>
<br>
Having "with" sometimes be a disguised "if" and sometimes a regular<br>
"with" will make it really, really hard to reason about code.<br>
<br>
<br>
--<br>
Steve<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div></blockquote></div>