Python,PEP-8和多行字典格式

时间:2014-06-16 15:53:08

标签: python pep8

我必须在Python中声明一些dict。我写了这样一段代码:

class MegaClass(object):
    _activation_grad_classes = \
    {
        activation.ForwardStrictRELU: activation.BackwardStrictRELU,
        activation.ForwardLog: activation.BackwardLog,
        activation.ForwardSinCos: activation.BackwardSinCos
    }

并收到PEP-8错误:E122 continuation line missing indentation or outdented

我该如何解决?

1 个答案:

答案 0 :(得分:10)

将左大括号放在作业行上:

_activation_grad_classes = {
    activation.ForwardStrictRELU: activation.BackwardStrictRELU,
    activation.ForwardLog: activation.BackwardLog,
    activation.ForwardSinCos: activation.BackwardSinCos
}

很少需要使用\来逃避Python中的换行符;而是使用{..}[..](..)将表达式分组到多行。