19
votes

Scenario: you have a long tuple as a result of a SQL query and want to unpack it into individual values. What's the best way to do that while conforming to PEP8? So far I have these three options:

  1. single assignment, use backslash to split to multiple lines

    person_id, first_name, last_name, email, \
        birth_date, graduation_year, home_street, \
        home_city, home_zip, mail_street, mail_city, \
        mail_zip = row
    
  2. single assignment, group left-hand side in parantheses and break lines without a backslash

    (person_id, first_name, last_name, email,
        birth_date, graduation_year, home_street,
        home_city, home_zip, mail_street, mail_city,
        mail_zip) = row
    
  3. split into multiple assignments, each fitting into a single line

    person_id, first_name, last_name, email = row[0:4]
    birth_date, graduation_year, home_street = row[4:7]
    home_city, home_zip, mail_street, mail_city = row[7:11]
    mail_zip = row[11]
    

Which of the three options is the best? Is there anything better?

2
Opinion based. I don't like the solution with the backslash at the end of a line because the code will break if you have an invisible character (e.g. space) after it. For me, the second version is the way to go. - Matthias
Have you considered a single namedtuple instead of separate names? - jonrsharpe
@Matthias He does ask "while conforming to PEP-8", and PEP-8 is pretty explicit about which to use. - chepner
Maybe you can wrap it into a dictionary. - Elmex80s
@jonrsharpe why did you vote to close this question as primarily opinion-based? The author of the question is not asking about unqualified preference, but he is asking whether any given approaches have certain advantages or disadvantages, and the answer to that is not subjective at all. The very first comment above is already a contradiction: Matthias says "Opinion based", only to follow up by providing an objective disadvantage of the backslash method. - Alex

2 Answers

22
votes

Anwering your question "Which of the three options is the best?"

pep8 states:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

This Means the second one is preferred over the first one. The third one is fine conforming pep8 as well, though personally wouldn't recommend it.

7
votes

To answer "is there anything better", I would suggest that a namedtuple allows you to access the individual data items with minimal fuss:

>>> from collections import namedtuple
>>> Person = namedtuple("Person", ['person_id', 'first_name', 'last_name', 
                                   'email', 'birth_date', 'graduation_year', 
                                   'home_street', 'home_city', 'home_zip', 
                                   'mail_street', 'mail_city', 'mail_zip'])
>>> row = range(12) # dummy data
>>> p = Person(*row) # unpack tuple into namedtuple
>>> p
Person(person_id=0, first_name=1, last_name=2, email=3, birth_date=4, graduation_year=5, home_street=6, home_city=7, home_zip=8, mail_street=9, mail_city=10, mail_zip=11)
>>> p.birth_date
4

This means you access attributes, rather than separate names, but is lighter-weight than building a class, keeps all of the data from your query together and exposes the values through sensible names.