我必须在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
。
我该如何解决?
答案 0 :(得分:10)
将左大括号放在作业行上:
_activation_grad_classes = {
activation.ForwardStrictRELU: activation.BackwardStrictRELU,
activation.ForwardLog: activation.BackwardLog,
activation.ForwardSinCos: activation.BackwardSinCos
}
很少需要使用\
来逃避Python中的换行符;而是使用{..}
,[..]
和(..)
将表达式分组到多行。