Python语句断行没有反斜杠

时间:2017-11-22 18:01:52

标签: python

如何在不使用" \"的情况下打破此行?

with mock.patch('six.moves.builtins.open', mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:
    #do something

我尝试使用括号,但它抱怨SyntaxError关于" as"

with (mock.patch('six.moves.builtins.open', mock.mock_open()),
    mock.patch('my_module.yaml.safe_load') as mock_yaml):
    #do something

1 个答案:

答案 0 :(得分:0)

在一组括号内打破它,但没有mock.patch(....)周围的括号,应该根据PEP8 Python Style Guide工作,因为括号之间有隐含的续行:

with mock.patch('six.moves.builtins.open', 
                mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:

另一个选项类似于您的第二个建议,但是在as之前移动了右括号:

with (mock.patch('six.moves.builtins.open', mock.mock_open()), 
      mock.patch('my_module.yaml.safe_load')) as mock_yaml: