I want to split python code over two lines, my code is something similar to:
if long_named_three_d_array[first_dimension][second_dimension][third_dimension] == somevalue:
//dosomething
I want to split above if condition over two lines.
Please help. Thanks.
In Python, the LHS can be bracketed.
>>> a = {}
>>> a[1] = {}
>>> a[1][2] = {}
>>> (a[1][2]
... [3]) = ''
>>> a
{1: {2: {3: ''}}}
>>> (b) = 2
>>> b
2
This means you can write your line as
if (long_named_three_d_array[first_dimension]
[second_dimension]
[third_dimension] ) == somevalue:
# Rest of code here, obviously properly indented in for the if.
You can use the line break continuation character, \
.
if long_named_three_d_array[first_dimension] \
[second_dimension]\
[third_dimension] == somevalue:
# Rest of code here, obviously properly indented in for the if.
One approach would be to use a temporary variable:
tmp = long_named_three_d_array[first_dimension][second_dimension][third_dimension]
if tmp == somevalue:
//dosomething
though shorter, yet descriptive variable identifiers would be a better choice if that's possible.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.