QuestionQ5Verified/Python/PyEZCopy linkReport a problemAsk AstroTutorSave questionWhich Python code block is an example of using a context manager?Awhile True: device "" Device(host-"vmx-1", user-"lab", passwd-"lab123") ...Btry: device "" Device(host-"vmx-1", user-"lab", passwd-"lab123") ... except: print("Unable to connect to the vMX1")Cwith Device(host="vmx-1", user="lab", passwd="lab123") as device: ...Dfor host in ["vmx-1", "vmx-2"]: device "" Device(host-host, user-"lab", passwd-"lab123") ...Show AnswerAnswerExplanationPython invokes context managers with a with statement. Its optional as clause binds the return value of __enter__() to a target, and __exit__() is invoked when execution leaves the block.Learn morePython documentation: The with statementPython documentation: With Statement Context ManagersShow comments2Show commentsCommunity DiscussionNo comments yet. Be the first to start the discussion!
Community Discussion