Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Python convention to avoid long lines of code? [closed]

Tags:

python

Should I create variables just to avoid having long lines of code? For example, in the code below the variable stream_records is only used once after it's set.

stream_records = stream.get_latest_records( num_records_to_correlate ).values('value')
stream_values = [float(record['value']) for record in stream_records]

Should I have done this instead?

stream_values = [float(record['value']) for record in stream.get_latest_records( num_records_to_correlate ).values('value')]

I am trying to optimize for readability. I'd love some opinions on whether it's harder to have to remember lots of variable names or harder to read long lines of code.

EDIT:

Another interesting option to consider for readability (thanks to John Smith Optional):

stream_values = [
    float(record['value'])
    for record in stream.get_latest_records(
        num_records_to_correlate
    ).values('value')
]
like image 649
Krishan Gupta Avatar asked May 17 '14 19:05

Krishan Gupta


1 Answers

PEP 8 is the style guideline from the beginning of Python, and it recommends that no source lines be longer than 79 characters.

Using a variable for intermediate results also performs a kind of documentation, if you do your variable naming well.

like image 161
Mark Ransom Avatar answered Sep 28 '22 11:09

Mark Ransom