【问题标题】:Python PEP-8: Assignment following E122 and E501Python PEP-8:E122 和 E501 之后的分配
【发布时间】:2015-11-09 17:05:56
【问题描述】:

在以下情况下,我应该如何改进多个或长变量的分配以遵循规则 E122 和 E501:

def my_example_function():
    return 1, 2, 3, 4
# How can I improve the following line:
first_variable, second_variable, third_variable, fourth_variable = my_example_function()

【问题讨论】:

    标签: python pep


    【解决方案1】:

    只需跨越多行..

    ( first_variable, second_variable, 
      third_variable, fourth_variable ) = my_example_function()
    

    【讨论】:

    • 这是一个相当简单的答案。我特别喜欢它,因为我的变量已经有大约 80 个字符长了。但我不知道为什么我忘记了。
    【解决方案2】:

    E122 says the line should be less than 80 characters, and E501 says that continuation lines should be indented. 重写该行的常用方法是,

    first_variable, second_variable, third_variable, fourth_variable = \   
        my_example_function()
    

    【讨论】:

    • 这是 C 和其他语言中的常用方法,但在 Python 中鼓励。除非有明显的原因,否则尽量避免使用大括号。
    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 2021-09-29
    • 1970-01-01
    • 2010-12-06
    • 2013-03-31
    • 1970-01-01
    • 2021-02-02
    • 2016-12-26
    相关资源
    最近更新 更多