<div dir="ltr">One last thing:<br><br>Since expressions tend to have pretty limited space, it might be worth it to consider more concise options like maybe instead of:<br><br><font face="monospace, monospace"><expression1> except (Exception[-list])[ as e]): <expression2></font><br><br>it could be:<br><br><font face="monospace, monospace"><expression1> (Exception[-list])[ as e])! <expression2><br></font><br>So:<br><br><font face="monospace, monospace">y = x[0] IndexError! default</font><div><br></div><div>instead of<br><br><font face="monospace, monospace">y = x[0] except IndexError: default</font><br><br>or:<br><br><font face="monospace, monospace">y = spam.ham[0] (AttributeError, IndexError) as e! foo(e)</font><br><br>instead of<br><font face="monospace, monospace"><br>y = spam.ham[0] except (AttributeError, IndexError) as e: foo(e)</font><br><br><font face="arial, helvetica, sans-serif">Or something to that effect. (thought I think it might be best to err on the side of 'except' because it's more clear what it does and it only adds 7 characters).<br></font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 3, 2018 at 6:57 PM, Abe Dillon <span dir="ltr"><<a href="mailto:abedillon@gmail.com" target="_blank">abedillon@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>[Benedikt Werner] </div><span class=""><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><span style="font-size:12.8px;text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">As the with-expression mimics the with-statement I would say this is similar to:<br></span><font face="monospace, monospace">with supress(AttributeError):<br>    tmp = <a href="http://person.name/" style="color:rgb(17,85,204)" target="_blank">person.name</a>[0]<br>initial = tmp                      # Error on assignment wouldn't get suppressed. Not relevant for this case but still.</font></blockquote><span style="font-size:12.8px;text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"></span><br class="m_8913356822146881806gmail-Apple-interchange-newline"></span>Ah, yes. That makes much more sense now. I would say the error on assignment IS relevant for that case and it's not clear to me how you would present that error to the user without causing a lot of confusion.<br><br>If it were implemented as:<br><br><font face="monospace, monospace">tmp = None<br>with suppress(AttributeError):<span class=""><br>    tmp = <a href="http://person.name" target="_blank">person.name</a>[0]<br>initial = tmp<br></span></font><br>Then it would solve many of the common use cases for the None-aware operators proposed in PEP 505, especially if we made it easy to filter out None-specific errors:<br><br><font face="monospace, monospace">class NoneError(BaseException):<br>    pass<br><br>class NoneAttributeError(<wbr>AttributeError, NoneError):<br>    pass<br><br>... code to make erroneous attribute access on NoneType objects throw NoneAttributeErrors ...<br>... shouldn't break much code other than maybe some doc-string tests ...<br><br>initial = <a href="http://person.name" target="_blank">person.name</a>[0] with suppress(NoneError)<br></font><br>[Robert Vanden Eynde]<span class=""><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px;text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">With implementation details? The highest value is there, the alg is clear., one fetches the filename, one preprocess the lines, then parse the data.</span></blockquote></span><div><br>Yes. Sorry for using confusing language. I was trying to say that I like your proposed syntax (for both with and except) because it follows the same principal. At the same time, I was trying to demonstrate part of the value of expression-ized statements to everyone else. Since Python is strictly evaluated statement by statement, there is no notion of looking ahead and re-ordering code. You can't use a variable in one statement then define that variable later on (like you do in comprehensions). Expressions, on the other hand; are parsed in a more complex way, so you can do things like put the relevant logic up front and move all the book-keeping to the end.<br><br><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">[Robert Vanden Eynde]<span class=""><div dir="auto" style="font-size:12.8px;text-decoration-style:initial;text-decoration-color:initial"><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><font face="sans-serif">About if elif elif else, ternary if does have that:</font><br><font face="monospace, monospace">y = (x+1 if x < 0 else<br>     x-1 if x > 0 else<br>     0)</font></blockquote></div><br></span>True, and your except syntax could chain in a similar manner. In-fact it might be possible to make a separate "finally" operator that executes the left side then executes the right side no matter what:<br><br><font face="monospace, monospace">y = ((x[0] except InxexError: x.default) except AttributeError: None) finally print("hello!")</font><br><br><span style="text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">[Robert Vanden Eynde]</span><span class=""><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">Limiting the scope is interesting, for "with" the only limitation in that the body must have exactly one assignment, like in the ternary if case?<br><font face="monospace, monospace">a = ([l.strip() for l in f.readlines()] with open ('name') as f)</font><br>By cutting the edge cases, "with something():" is not possible, only "with ... as" being possible?</blockquote><br></span></span>I think it's better to forget what I said about limiting the scope of the operator. It was a half-baked thought.<br><br>As for assignment expressions, they're already on their way thanks to PEP 572. I like the "where" version you proposed because it places the logic first, but unfortunately PEP 572 (<a href="https://www.python.org/dev/peps/pep-0572/#alternative-spellings" target="_blank">4th alternate spelling</a>) states that they had considered that version and rejected it because "where" is a pretty common variable name. From the PEP:<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;text-align:left;background-color:rgb(249,249,249);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">SQLAlchemy and numpy have </span><tt class="m_8913356822146881806gmail-docutils m_8913356822146881806gmail-literal" style="box-sizing:border-box;color:rgb(68,68,68);text-align:left;background-color:rgb(249,249,249);text-decoration-style:initial;text-decoration-color:initial">where</tt><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;text-align:left;background-color:rgb(249,249,249);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><span> </span>methods, as does<span> </span></span><tt class="m_8913356822146881806gmail-docutils m_8913356822146881806gmail-literal" style="box-sizing:border-box;color:rgb(68,68,68);text-align:left;background-color:rgb(249,249,249);text-decoration-style:initial;text-decoration-color:initial">tkinter.dnd.Icon</tt><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;text-align:left;background-color:rgb(249,249,249);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"> in the standard library</span></blockquote><div><br>So adding a new "where" keyword would break backwards compatibility in a major way. </div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Fri, Aug 3, 2018 at 5:40 PM, Benedikt Werner <span dir="ltr"><<a href="mailto:1benediktwerner@gmail.com" target="_blank">1benediktwerner@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
  
    
  
  <div text="#000000" bgcolor="#FFFFFF"><span>
    <font face="monospace, monospace">
      <blockquote type="cite">For instance, what would the following do?<br>
        <br>
        <font face="monospace, monospace">initial = <a href="http://person.name" target="_blank">person.name</a>[0] with
          suppress(AttributeError)  # Hangover from PEP 505
          discussion...</font></blockquote>
    </font></span>As the with-expression mimics the with-statement I would say
    this is similar to:<br>
    <pre>with supress(AttributeError):
    tmp = <a href="http://person.name" target="_blank">person.name</a>[0]
initial = tmp                   # Error on assignment wouldn't get suppressed. Not relevant for this case but still.
</pre>
    I don't understand where this is ambigous?<span><br>
    <br>
    <blockquote type="cite">
      <div dir="ltr">So maybe it only makes sense to use expression
        assignment (PEP 572):<br>
        <div>
          <div><br>
            data = (d := file.read() with open(...) as file)<br>
            <br>
            To which I say, "Eww..."<br>
          </div>
        </div>
      </div>
    </blockquote></span>
    I definitely agree that this looks a bit bad but I don't get why you
    would consider using an expression assignment there.<br>
    <pre>data = file.read with open(...) as file</pre>
    works perfectly fine so why would you want to additonaly assign it
    to another variable "d"?<br>
    <br>
    <br>
    Overall I like the idea of the with-expression as it allows you to
    make some common use cases like the open/read example more readable.
    It's clear at first sight that what is actually done is reading the
    file. While it's true that it's usually easy to write a simple
    function to do the job that still isn't as simple to understand and
    in most cases when reading the code you then have to look at the
    function to see if anything else is done there. Also what if you
    then do readlines somewhere? Then you need another function.<br>
  </div>

<br></div></div><span class="">______________________________<wbr>_________________<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/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
<br></span></blockquote></div><br></div>
</blockquote></div><br></div>