0
import contextlib
from win32com.client import Dispatch  

@contextlib.contextmanager
def excel_ctx() -> Generator[Dispatch, None, None] :
    try:
        yield excel := Dispatch("Excel.Application")
    finally:
        excel.quit()

Is meant to create a new Excel App and call its quit method every time. I thought it could be written more concisely with the walrus operator, but I get SyntaxError: invalid syntax

1 Answer 1

2

You need to add parenthesis around the walrus operator like this:

yield (excel := Dispatch("Excel.Application"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.