当出现点或逗号时如何在PEP8中使用反斜杠?

问题描述 投票:0回答:1

这是我的数据框:

In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'col1':['A','A','A','B','B','B'], 'col2':['C','D','D','D','C','C'], 
            'col3':[.1,.2,.4,.6,.8,1]})
In [3]: df
Out[4]: 
  col1 col2  col3
0    A    C   0.1
1    A    D   0.2
2    A    D   0.4
3    B    D   0.6
4    B    C   0.8
5    B    C   1.0

我的问题是:当我想包装长文本时,应在反斜杠放在哪里?点之后还是点之前?哪个正确?

# option 1 backslash after dot or comma
df.groupby('col1').\
    sum()
df['col1'],\
    df['col2']

# option 2 backslash before dot or comma
df.groupby('col1')\
    .sum()
df['col1']\
    ,df['col2']

我还发现,如果使用括号,则不必使用反斜杠。那么哪个选项是正确的?

# option 1: no backslash and dot or comma in the new line
(df.groupby('col1')
    .sum())
(df['col1']
    ,df['col2'])

# option 2: no backslash and dot or comma in the old line
(df.groupby('col1').
    sum())
(df['col1'],
    df['col2'])

# option 3: backslash after dot or comma 
(df.groupby('col1').\
    sum())
(df['col1'],\
    df['col2'])

# option 4: backslash before dot or comma 
(df.groupby('col1')\
    .sum())
(df['col1']\
    ,df['col2'])
python-3.x backslash pep8
1个回答
1
投票

说明

PEP8 prefers usage of brackets over backslashes where it's possible

PEP8并没有说点或逗号必须与表达式在同一行(尽管在给出的每个示例中都一样)。

解决方案

技术上正确的答案是:

# option 1: no backslash and dot or comma in the new line
(df.groupby('col1')
    .sum())
(df['col1']
    ,df['col2'])

# option 2: no backslash and dot or comma in the old line
(df.groupby('col1').
    sum())
(df['col1'],
    df['col2'])

尽管one could argue that the recommendation against space between a trailing comma and closing parenthesis is the exception that proves the rule,这意味着:,df['col2']不遵循标准(尽管, df['col2']仍然遵循)。

尽管以上介绍的两个选项在技术上都是正确的,但以下是最常用

(df.groupby('col1')
    .sum())
(df['col1'],
    df['col2'])

注意:indentation取决于使用的上下文。以上示例不应作为参考。还请记住,PEP8 is just a guideline在许多情况下都应打破规则以提高可读性。

© www.soinside.com 2019 - 2024. All rights reserved.