Home > Mobile >  How to write the return in multiple lines
How to write the return in multiple lines

Time:08-10

The following function has many items in the returned tuples:

def test_real():
    return (item_a_long_name, item_b_long_name), (item_c_long_name, item_d_long_name), ..., (item_x_long_name, item_y_long_name)

It is hard to see all the contents in the Jupyter Notebook Cell as the return will display in a single line.

The following code is trying to split the return items:

def test():
    return (1,2), (3,4)

print("test:", test())

def test2():
    return (
        1,2
    ), 
    (
        3,4
    )

print("test2:", test2())

Here is the output:

test: ((1, 2), (3, 4))
test2: ((1, 2),)

The test2 is not work as expected. How to write the correct return in the test2?

CodePudding user response:

Just add extra parenthesis to wrap the return properly then the return is spread across multiple lines.

def test():
    return (1,2), (3,4)

print("test:", test())

def test2():
    return ((
        1,2
    ), 
    (
        3,4
    ))

print("test2:", test2())
# output
test: ((1, 2), (3, 4))
test2: ((1, 2), (3, 4))
  • Related