Fichas
python beyond context manager
Fichas del conjunto (10)
- 1what is a context manageran object designed to be used in a with statement
- 2what methods are used by a context manager2 methods (enter and exit)
with context-manager:
enter()
///body///
exit()
__enter__(self)
and
__exit__(self,exc_type,exc_val,exc_tb) - 3given the following code: with myexpression as x: body what must myexpression do?myexpression must evaluate to a context manager, in other words, it must produce an object that implements:
__enter__()
and
__exit__ ()
methods - 4what is bound to x : with myexpression as x:the return value of the __enter__() method
- 5in __exit__() context manager method, what is exc_type, exc_val, and exc_tb ?exc_type is exception type
exc_val is exception object
exc_tb is exception traceback - 6how can you see if there was an exception thrown in a context managerinside __exit__( self, exc_type, exc_val, exc_tb )
check exc_type for something other than None to see if exception was thrown - 7what is a standard library for working with context managers?contextlib
- 8what is the decorator inside contextlib that can be used to create new context managerscontextlib.contextmanager
- 9what is the general code outline for a context manager using contextlib.contextmanager@contextlib.contextmanager
def my_context_manager():
#
try: '
yield [value]
#
except: '
#
raise ' - 10can you use multiple context manager?>yes
with cm1() as a, cm2() as b, ...:
BODY
Información
- Descripción
- (beyond basics python flashcards) python Fichas sobre python beyond context manager, creado por Andrew Sperazza el 12/01/2017.
- Creado por
- Andrew Sperazza
- Visitas
- 1
- Valoración