Assessment Question: Python with-statement

Sep 8, 2013 • Philipp Lies

Entry level:
You want to read data from a file. What’s the recommended way to do it:

A) Use the open(…) function
B) Check if the file exists, then open it
C) Use the with statement
D) Open the file inside a try block and catch IO exceptions

 

Experienced level:
You want to write your own class to use with the with-statement. Which functions does your class have to provide:

A) __create__ and __delete__
B) __enter__ and __exit__
C) __init__ and __del__
D) __start__ and __end__