根据 PEP8 标准和 python 换行技术分解字符串格式代码行的最佳方法是什么?

What would be the best way to break up a string format line of code based on PEP8 standards and python line breaking techniques?

我们的新 CTO 希望我们使用 PEP8 标准格式化所有代码。这包括 Python 行不超过 80 到最多 100 个字符的代码。我有这行扩展代码。关于如何将此行分成 2-3 条干净、可读的行的任何提示。

    # Identify if Authenticated 
    identify = ''
    if is_authenticated:
        fullname = request.user.full_name if 'full_name' in request.user else ''
        identify = 'mixpanel.identify("{} ");\nmixpanel.people.set({{"$email": {email}, "$name": {name}}})'.format(email=request.user.email, name=fullname)

我已经尝试过字符串中断,例如:

    # Identify if Authenticated 
    identify = ''
    if is_authenticated:
        fullname = request.user.full_name if 'full_name' in request.user else ''
        identify = 'mixpanel.identify("{} ");\n'
        'mixpanel.people.set({{"$email": {email}, "$name": {name}}})'.format(email=request.user.email, name=fullname)

... 但是当我引用另一个 SO post 说这没问题时,linting 返回了一个无法识别格式的错误(红色波浪形)。

我还考虑过将所有内容都以 .format 开头。

有什么建议吗?

我会利用括号:

    identify = ''
    if is_authenticated:
        fullname = request.user.full_name if 'full_name' in request.user else ''
        form = (
            'mixpanel.identify("{} ");\n'
            'mixpanel.people.set({{"$email": {email}, "$name": {name}}})'
        )
        identify = form.format(email=request.user.email, name=fullname)

我最终将传递给 textwrap.dedent 的三重引号和结果的调用格式。 textwrap.dedent("""\ 所需的行数 """).format(...)。三元组开头的行延续是为了抑制空白的第一行。我也喜欢在文档字符串中使用它。