长元组拆解的习语 [英] Idiom for long tuple unpacking

查看:89
本文介绍了长元组拆解的习语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案:由于SQL查询,您有一个长元组,并希望将其拆成单独的值。符合PEP8的最佳方法是什么?到目前为止,我有以下三种选择:

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. 单个分配,使用反斜杠拆分成多行

  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


  • 单个分配,组左侧在括号和不带反斜杠的换行符中

  • 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
    


  • 分为多个分配,每个分配适合一行

  • 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?

    推荐答案

    回答您的问题三个选项中的哪一个最好?

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

    pep8 状态:


    包裹长行的首选方法是在括号,方括号和花括号内使用Python的隐含行连续性。通过将表达式包装在括号中,可以将长行分成多行。应该优先使用这些,而不是使用反斜杠来继续行。

    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.

    这意味着第二个优先于第一个。第三个也很好地符合pep8,尽管个人不建议这样做。

    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.

    这篇关于长元组拆解的习语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆