QuestionQ5

Python/PyEZ

Which Python code block is an example of using a context manager?

  • A while True: device "" Device(host-"vmx-1", user-"lab", passwd-"lab123") ...
  • B try: device "" Device(host-"vmx-1", user-"lab", passwd-"lab123") ... except: print("Unable to connect to the vMX1")
  • C with Device(host="vmx-1", user="lab", passwd="lab123") as device: ...
  • D for host in ["vmx-1", "vmx-2"]: device "" Device(host-host, user-"lab", passwd-"lab123") ...
Explanation

Python 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 more

Community Discussion

No comments yet. Be the first to start the discussion!