Is there a convention in Python to avoid long lines of code?

Should I create variables to avoid long lines of code? For example, in the code below, a variable is stream_recordsused only once after setting it.

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

Should I do 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 would like some opinions on whether to remember many variable names or whether it is more difficult to read long lines of code.

EDIT:

Another interesting opportunity to consider reading (thanks to John Smith Optional):

stream_values = [
    float(record['value'])
    for record in stream.get_latest_records(
        num_records_to_correlate
    ).values('value')
]
+4
source share
4 answers

PEP 8 is a style directive from the beginning of Python and recommends that source strings not exceed 79 characters.

, .

+4

, , , , , , . , - ( ) .

+2

( ) . , . - :

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

\, . :

long_variable_name = object1.attribute1.method1(arg1, arg2, arg3) + \
 object2.attribute2.method2(arg1, arg2, arg3)
+1

- . , . , , , get_latests_records .

map:

stream_values = map(lambda x: float(x['value']), 
                    stream.get_latest_records(num_records_to_correlate) \
                    .values('value')])

lambda float . , , PEP8, .

0

Source: https://habr.com/ru/post/1540984/


All Articles