Search code examples
pythonstylescontextmanager

Multiline with clause in Python


How can I spread a long with clause in Python over multiple lines? Right now, I have

    with self.context_manager_one(some, parameters, that, are, passed) \
            as return_value_one, \
            self.context_manager_two(self.p, slice(None), None) \
            as return_value_two:

I would like to follow Google's Python style guide, which proscribes backslash line continuation.


Solution

  • This is fixed in Python 3.10!

    https://github.com/we-like-parsers/pegen/issues/229

    https://bugs.python.org/issue12782