Skip to content Skip to sidebar Skip to footer

How To Cleanly Keep Below 80-char Width With Long Strings?

I'm attempting to keep my code to 80 chars or less nowadays as I think it looks more aesthetically pleasing, for the most part. Sometimes, though, the code ends up looking worse if

Solution 1:

You can split the string into two:

def foo():
    ifconditional():
        logger.info("<Conditional's meaning> happened, so we're not ""setting up the interface.")

Multiple consecutive strings within the same expression are automatically concatenated into one, at compile time:

>>>deffoo():...if conditional():...        logger.info("<Conditional's meaning> happened, so we're not "..."setting up the interface.")...>>>import dis>>>dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (conditional)
              3 CALL_FUNCTION            0
              6 POP_JUMP_IF_FALSE       25

  3           9 LOAD_GLOBAL              1 (logger)
             12 LOAD_ATTR                2 (info)
             15 LOAD_CONST               1 ("<Conditional's meaning> happened, so we're not setting up the interface.")
             18 CALL_FUNCTION            1
             21 POP_TOP             
             22 JUMP_FORWARD             0 (to 25)
        >>   25 LOAD_CONST               0 (None)
             28 RETURN_VALUE        

Note the LOAD_CONST for line 3, the bytecode for the function contains one string, already concatenated.

If you were to add a + to the expression, two separate constants are created:

>>>deffoo():...if conditional():...        logger.info("<Conditional's meaning> happened, so we're not " + ..."setting up the interface.")...>>>dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (conditional)
              3 CALL_FUNCTION            0
              6 POP_JUMP_IF_FALSE       29

  3           9 LOAD_GLOBAL              1 (logger)
             12 LOAD_ATTR                2 (info)
             15 LOAD_CONST               1 ("<Conditional's meaning> happened, so we're not ")

  4          18 LOAD_CONST               2 ('setting up the interface.')
             21 BINARY_ADD          
             22 CALL_FUNCTION            1
             25 POP_TOP             
             26 JUMP_FORWARD             0 (to 29)
        >>   29 LOAD_CONST               0 (None)
             32 RETURN_VALUE        

Python does fold binary operations on constants at compile time (so +, *, - etc.), in the peephole optimizations for the byte compiler. So for certain string concatenations the compiler may also replace + string concatenation of constants with the concatenated result. See peephole.c, for sequences (including strings) this optimization is only applied if the result is limited to 20 items (characters) or fewer.

Post a Comment for "How To Cleanly Keep Below 80-char Width With Long Strings?"