[Python-ideas] if expensive_computation() as x:

Terry Reedy tjreedy at udel.edu
Fri Feb 14 23:02:10 CET 2014


On 2/14/2014 4:06 PM, Chris Angelico wrote:
> On Sat, Feb 15, 2014 at 7:48 AM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Fri, Feb 14, 2014 at 05:18:55AM -0500, Terry Reedy wrote:
>>
>>> I do not really understand the fear of indents that would cause one to
>>> repeat calculations rather than write the actual logic.

In C, where (syntactically insignificant) 8-space tabs are standard, I 
can, but not in Python where 4 spaces are standard and one can use less.

>>>      x = expensive_computation_0():
>>>      if x:
>>>          # Do something with x...
>>>      else:
>>>          x = expensive_computation_1()
>>>          if x:
>>>              # Do something with x...
>>
>> That's really not very nice looking.

It is an accurate representation of the logic, so you are saying that 
the logic is not nice looking. Ok.

 > It's okay with one or two levels, three at the most,

As I said, but you clipped.

> More to the point, excessive _and inappropriate_ indentation. An
> if/elif/elif/elif/else chain puts all the conditions at the same
> level, and all the bodies at the same level (one further in than the
> conditions). The intention is that they're all peers,

But they are *not* peers.

> not that they're nested inside each other.

But logically, they are.

> Since Python, unlike C, doesn't let you assign inside a condition

The C construction with a side-effect expression as a condition is a bit 
of a contortion. Its main virtue is avoidance of indents.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list