Best way to split up lines - RE: About the 79 character line recommendation

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Thu Dec 7 13:55:41 EST 2006


Michael Yanowitz schreef:
> Hello:
> 
>    I too don't like large lines. However in the following case, and
> multi-level indentations, I find it unavoidable.
>    Here is one huge statement I haven't been able to split onto multiple
> lines.
> What would be the best way to split the following line (Python doesn't like
> me
> to split it up between the comma-separated parameters):
> 
>     top, ip1, ip2, ip3, ip4, messageCounter, ackRequired, dataType, utc1,
> utc2, utc3, utc4, utc5, utc6, utc7, utc8, utc9, utc10, utc11, utc12, st1,
> st2, st3, st4, st5, st6, numberOfLabels, dataWord =
> struct.unpack("!H4BH20BHI", strMessage)

I see three possibilities:

1. Avoid the one-liner alltogether

u = struct.unpack("!H4BH20BHI", strMessage)
top = u[0]
ip1, ip2, ip3, ip4 = u[1:5]
messageCounter = u[6]
# ... and so on ...

In this approach with your example it seems to me to be a good idea to 
put ip, utc and st in separate tuples like this:

u = struct.unpack("!H4BH20BHI", strMessage)
top = u[0]
ip = u[1:5]
messageCounter = u[5]
ackRequired = u[6]
dataType = u[7]
utc = u[8:20]
st = u[20:26]
numberOfLabels = u[26]
dataWord = u[27]


2. Use parens around the tuple on the left hand side

(top, ip1, ip2, ip3, ip4, messageCounter, ackRequired, dataType, utc1,
utc2, utc3, utc4, utc5, utc6, utc7, utc8, utc9, utc10, utc11, utc12, 
st1, st2, st3, st4, st5, st6, numberOfLabels,
dataWord) = struct.unpack("!H4BH20BHI", strMessage)


3. Use '\' to break the line

top, ip1, ip2, ip3, ip4, messageCounter, ackRequired, dataType, utc1, \
utc2, utc3, utc4, utc5, utc6, utc7, utc8, utc9, utc10, utc11, \
utc12, st1, st2, st3, st4, st5, st6, numberOfLabels, dataWord \
  = struct.unpack("!H4BH20BHI", strMessage)



-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven



More information about the Python-list mailing list