Miguel Lebon
Miguel Lebon

Reputation: 49

How to split a return statement in several lines to fulfill flake8 style

I have a function with a return statement like:

return apple, banana, orange, pear, grape, kiwi

But it exceeds the maximum number of characters that flake8 lets me (in the example I've just put a dummy example), so how should I do it?

I've tried things like

return apple, banana, orange,\
    pear, grape, kiwi

return apple, banana, orange,
    pear, grape, kiwi

return apple, banana, orange,
pear, grape, kiwi

And nothing works. The last one was accepted by flake8 but it throws an execution error.

Thank you very much for your help

Upvotes: 0

Views: 470

Answers (2)

anthony sottile
anthony sottile

Reputation: 69934

you didn't specify the error codes you're seeing -- flake8 doesn't implement any itself, but you're likely seeing codes from pycodestyle (either E / W)

pycodestyle accepts any of these:

def f():
    return apple, banana, orange,\
        pear, grape, kiwi
def f():
    return (
        apple, banana, orange,
        pear, grape, kiwi,
    )
def f():
    return (
        apple,
        banana,
        orange,
        pear,
        grape,
        kiwi,
    )

disclaimer: I'm the current flake8 maintainer and one of the pycodestyle maintainers

Upvotes: 1

basckerwil
basckerwil

Reputation: 388

You return a tuple, so you can do like this:

return (
    apple,
    banana,
    orange,
    pear,
    grape,
    kiwi
)

Upvotes: 2

Related Questions