Line continuation and comments

dn PythonList at DancesWithMice.info
Thu Feb 23 17:38:30 EST 2023


On 22/02/2023 21.49, Robert Latest via Python-list wrote:
> I found myself building a complicated logical condition with many ands and ors
> which I made more manageable by putting the various terms on individual lines
> and breaking them with the "\" line continuation character. In this context it
> would have been nice to be able to add comments to lines terms which of course
> isn't possible because the backslash must be the last character on the line.
> 
> Question: If the Python syntax were changed to allow comments after line-ending
> backslashes, would it break any existing code? I can't think of an example.

Alternative to suggestions thus far: break the complex* logical 
condition into 'labelled' components, and then combine those (likely 
shorter) into the condition:

if person.is_adult and person.is_qualified and person.has_funds ...

which presumes that at some previous time we have, for example:

def is_adult( self )->bool:
     return 21 <= self.age <= 65

(not that I'd like to see those two 'magic-constants' in anyone's code, 
but (hopefully) the idea has been conveyed...)


* "simple is better than..."

NB my PyCharm-settings grumble whenever I create an identifier which is 
only used once (and perhaps, soon after it was established). I 
understand the (space) optimisation, but prefer to trade that for 
'readability'.
-- 
Regards,
=dn


More information about the Python-list mailing list