raghavsikaria
raghavsikaria

Reputation: 1096

How to format the a long line in python where a function call returns multiple outputs and I have to receive them?

So I have the following line in python:

for category in SomeLongClass.license_categories:
    data[f"Category {category}"], data[f"Category {category} Validity Date"], data[f"Category {category} Expiry Date"] = SomeLongClass.get_category_dates(text_lines=text_lines, category=category)

And as you can see this line is long and compromises on readability. I ran pylint on this file, and it just tells me that this line is long, but not how to remedy it. I tried googling it out, it says to just put a \ and break in a new line, but isn't that going to compromise readability more in this case?

How to go about formatting this line?

Upvotes: 0

Views: 128

Answers (3)

Ajay A
Ajay A

Reputation: 1068

Try assigning to different variables and then assign to those variables

for category in SomeLongClass.license_categories:
    a1, a2, a3 = SomeLongClass.get_category_dates(text_lines=text_lines,
                                                  category=category)
    data[f"Category {category}"] = a1
    data[f"Category {category} Validity Date"] = a2
    data[f"Category {category} Expiry Date"] = a3

Upvotes: 0

Ekure Edem
Ekure Edem

Reputation: 330

I think you want the line breaks. It should be

for category in SomeLongClass.license_categories:
    (data[f"Category {category}"], \
    data[f"Category {category} Validity Date"], \
    data[f"Category {category} Expiry Date"]) = SomeLongClass.get_category_dates(text_lines=text_lines, category=category)

Upvotes: 0

Prune
Prune

Reputation: 77857

This is one way that my group at work would have done it: align the receiving variables, align the line continuations (not strictly PEP-8), and indent the RHS.

# Assign three return values to the desired variables.
data[f"Category {category}"],               \
data[f"Category {category} Validity Date"], \
data[f"Category {category} Expiry Date"] =  \
    SomeLongClass.get_category_dates(text_lines=text_lines, category=category)

Upvotes: 1

Related Questions