<story><title>New Ways to Be Told That Your Python Code Is Bad</title><url>https://nickdrozd.github.io/2021/09/02/new-pylint-checks.html</url></story><parent_chain><item><author>ohazi</author><text>&amp;gt; Python programmers in general have an irrational aversion to if-expressions, a.k.a. the “ternary” operator.&lt;p&gt;Because the Python ternary operator is fucking backwards! Why on earth would you put the condition in the &lt;i&gt;middle&lt;/i&gt;!?&lt;p&gt;&lt;pre&gt;&lt;code&gt;  x = 4 if condition() else 5
&lt;/code&gt;&lt;/pre&gt;
vs.:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  condition() ? 4 : 5
&lt;/code&gt;&lt;/pre&gt;
vs. the author&amp;#x27;s own lisp example:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  (setq x (if (condition) 4 5))
&lt;/code&gt;&lt;/pre&gt;
I love ternary operators, and Lisp&amp;#x2F;ML-style if else blocks that return things, but Python puts the 4 and the 5 so far away from each other that even I hate using it, especially when condition is a chained mess rather than just a function call.</text></item></parent_chain><comment><author>Uberphallus</author><text>I love Python but my personal hate goes for this when used in list comprehensions.&lt;p&gt;&lt;pre&gt;&lt;code&gt;    a = [1,2,3]
    # list comprehension with if
    [ x for x in a if x &amp;gt; 1]
    [2, 3]
    # list comprehension with if&amp;#x2F;else
    [ x if x &amp;gt; 1 else x*2 for x in a]
    [2, 2, 3]
&lt;/code&gt;&lt;/pre&gt;
When it&amp;#x27;s just &amp;quot;if&amp;quot; it goes after the &amp;quot;for&amp;quot;, when it&amp;#x27;s &amp;quot;if&amp;#x2F;else&amp;quot; it goes, all of it, before. I still don&amp;#x27;t understand why it&amp;#x27;s this way, it doesn&amp;#x27;t even make sense even reading it in natural language. I much prefer the mathematical-like syntax present in Scala for this.&lt;p&gt;Edit: Thanks for the responses, they are are insightful, never thought of this that way.&lt;p&gt;Still in my head the lack of &amp;quot;else&amp;quot; clause implies filtering no matter where, while its presence implies transformation no matter where. The first element of the list is always transformed (though in this example it&amp;#x27;s identity).&lt;p&gt;I think it&amp;#x27;s a case of Python trying to do too much with too little. Personally for anything relatively complicated I still prefer to use filter() and map() as list comprehensions can get unwieldly quick.</text></comment>
<story><title>New Ways to Be Told That Your Python Code Is Bad</title><url>https://nickdrozd.github.io/2021/09/02/new-pylint-checks.html</url></story><parent_chain><item><author>ohazi</author><text>&amp;gt; Python programmers in general have an irrational aversion to if-expressions, a.k.a. the “ternary” operator.&lt;p&gt;Because the Python ternary operator is fucking backwards! Why on earth would you put the condition in the &lt;i&gt;middle&lt;/i&gt;!?&lt;p&gt;&lt;pre&gt;&lt;code&gt;  x = 4 if condition() else 5
&lt;/code&gt;&lt;/pre&gt;
vs.:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  condition() ? 4 : 5
&lt;/code&gt;&lt;/pre&gt;
vs. the author&amp;#x27;s own lisp example:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  (setq x (if (condition) 4 5))
&lt;/code&gt;&lt;/pre&gt;
I love ternary operators, and Lisp&amp;#x2F;ML-style if else blocks that return things, but Python puts the 4 and the 5 so far away from each other that even I hate using it, especially when condition is a chained mess rather than just a function call.</text></item></parent_chain><comment><author>rcthompson</author><text>I think the Python ternary is best read as &amp;quot;DEFAULT_VALUE if COMMON_CASE else FALLBACK&amp;quot;, and is best used in situations where this is what is meant semantically. If you&amp;#x27;re dealing with two equally plausible code paths, the standard if&amp;#x2F;else branching structure makes that more clear.</text></comment>