<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Toshi Kurauchi</title>
    <description>The latest articles on DEV Community by Toshi Kurauchi (@toshikurauchi).</description>
    <link>https://dev.to/toshikurauchi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F223098%2Fdda2ae0d-2db5-4831-be67-0fb3715d3108.jpeg</url>
      <title>DEV Community: Toshi Kurauchi</title>
      <link>https://dev.to/toshikurauchi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/toshikurauchi"/>
    <language>en</language>
    <item>
      <title>Python 3.8's Walrus Operator is (almost) perfect for Jupyter Notebooks</title>
      <dc:creator>Toshi Kurauchi</dc:creator>
      <pubDate>Sat, 29 Feb 2020 19:36:50 +0000</pubDate>
      <link>https://dev.to/toshikurauchi/python-3-8-s-walrus-operator-is-almost-perfect-for-jupyter-notebooks-4n3j</link>
      <guid>https://dev.to/toshikurauchi/python-3-8-s-walrus-operator-is-almost-perfect-for-jupyter-notebooks-4n3j</guid>
      <description>&lt;p&gt;Assignment expressions, AKA the &lt;a href="https://realpython.com/lessons/assignment-expressions/"&gt;walrus operator&lt;/a&gt;, is (arguably) one of the coolest new features of Python 3.8. Admittedly, I first loved it simply because its name shows that goofiness remains strong in the dev community.&lt;/p&gt;

&lt;p&gt;Then I started thinking about possible use cases for it. I read some articles, reflected a little more, and ended up with the impression that it wasn't a bad feature, but wasn't really that useful.&lt;/p&gt;

&lt;p&gt;Fast forward to this week. I was teaching a class about basic Jupyter Notebook usage when it struck me that the walrus operator is great for notebooks! &lt;/p&gt;

&lt;p&gt;How often do we encounter cells such as these:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9LnIvDa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4g838riomiq9hsb9c5o7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9LnIvDa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4g838riomiq9hsb9c5o7.png" alt="Jupyter notebook cell with a line of Python code: variable = do_something() followed by another line of Python code: variable, followed by a DataFrame showing the result."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Essentially, what we want is to assign a value to a variable and then return it to be displayed as the result of the cell. Well, it turns out that that's exactly what the walrus operator is for: assign and return. So now we can have a more concise cell with:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GZXsyUth--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l8sbo0gjnwwdwfqpxr4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GZXsyUth--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l8sbo0gjnwwdwfqpxr4t.png" alt="Jupyter notebook cell with Python code: (variable := do_something()) followed by a DataFrame showing the result."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you can probably see why the title says &lt;strong&gt;almost&lt;/strong&gt; perfect. It would be nice if this simpler version worked:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kkxWK97n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bqi85wnbmdud7n1ort55.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kkxWK97n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bqi85wnbmdud7n1ort55.png" alt="Jupyter notebook cell with Python code: variable := do_something() followed by a SyntaxError."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However it doesn't, and such is life. Apparently there has been &lt;a href="https://bugs.python.org/issue35224"&gt;some discussion over this&lt;/a&gt; and it will not be supported. So we will have to live with those parentheses for now.&lt;/p&gt;

&lt;p&gt;It may still not be that exciting, but I'm happier with the walrus operator after finding this use case, so I decided to share it. Also, before writing this post I did some research and found messages from &lt;a href="https://bugs.python.org/msg339098"&gt;Vedran Čačić&lt;/a&gt; and &lt;a href="https://bugs.python.org/msg339131"&gt;Carol Willing&lt;/a&gt; discussing this use case, so I thought it would be nice to acknowledge them for bringing this up.&lt;/p&gt;

</description>
      <category>python</category>
      <category>jupyter</category>
    </item>
  </channel>
</rss>
