GoConqr has a new look! Let us know what you think.

Flashcards

python beyond context manager

Andrew Sperazza python English 10 cards

Cards in this deck (10)

  1. 1what is a context manageran object designed to be used in a with statement
  2. 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)
  3. 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
  4. 4what is bound to x : with myexpression as x:the return value of the __enter__() method
  5. 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
  6. 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
  7. 7what is a standard library for working with context managers?contextlib
  8. 8what is the decorator inside contextlib that can be used to create new context managerscontextlib.contextmanager
  9. 9what is the general code outline for a context manager using contextlib.contextmanager@contextlib.contextmanager
    def my_context_manager():
    #
    try: '
    yield [value]
    #
    except: '
    #
    raise '
  10. 10can you use multiple context manager?>yes
    with cm1() as a, cm2() as b, ...:
    BODY

Information

Description
python (beyond basics python flashcards) Flashcards on python beyond context manager, created by Andrew Sperazza on 12/01/2017.
Created by
Andrew Sperazza
Views
1
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?