What Is the Python "With" Statement Designed For

What is the python with statement designed for?

  1. I believe this has already been answered by other users before me, so I only add it for the sake of completeness: the with statement simplifies exception handling by encapsulating common preparation and cleanup tasks in so-called context managers. More details can be found in PEP 343. For instance, the open statement is a context manager in itself, which lets you open a file, keep it open as long as the execution is in the context of the with statement where you used it, and close it as soon as you leave the context, no matter whether you have left it because of an exception or during regular control flow. The with statement can thus be used in ways similar to the RAII pattern in C++: some resource is acquired by the with statement and released when you leave the with context.

  2. Some examples are: opening files using with open(filename) as fp:, acquiring locks using with lock: (where lock is an instance of threading.Lock). You can also construct your own context managers using the contextmanager decorator from contextlib. For instance, I often use this when I have to change the current directory temporarily and then return to where I was:

    from contextlib import contextmanager
    import os

    @contextmanager
    def working_directory(path):
    current_dir = os.getcwd()
    os.chdir(path)
    try:
    yield
    finally:
    os.chdir(current_dir)

    with working_directory("data/stuff"):
    # do something within data/stuff
    # here I am back again in the original working directory

    Here's another example that temporarily redirects sys.stdin, sys.stdout and sys.stderr to some other file handle and restores them later:

    from contextlib import contextmanager
    import sys

    @contextmanager
    def redirected(**kwds):
    stream_names = ["stdin", "stdout", "stderr"]
    old_streams = {}
    try:
    for sname in stream_names:
    stream = kwds.get(sname, None)
    if stream is not None and stream != getattr(sys, sname):
    old_streams[sname] = getattr(sys, sname)
    setattr(sys, sname, stream)
    yield
    finally:
    for sname, stream in old_streams.iteritems():
    setattr(sys, sname, stream)

    with redirected(stdout=open("/tmp/log.txt", "w")):
    # these print statements will go to /tmp/log.txt
    print "Test entry 1"
    print "Test entry 2"
    # back to the normal stdout
    print "Back to normal stdout again"

    And finally, another example that creates a temporary folder and cleans it up when leaving the context:

    from tempfile import mkdtemp
    from shutil import rmtree

    @contextmanager
    def temporary_dir(*args, **kwds):
    name = mkdtemp(*args, **kwds)
    try:
    yield name
    finally:
    shutil.rmtree(name)

    with temporary_dir() as dirname:
    # do whatever you want

What does the 'with' statement do in python?

with statements open a resource and guarantee that the resource will be closed when the with block completes, regardless of how the block completes. Consider a file:

with open('/etc/passwd', 'r') as f:
print f.readlines()

print "file is now closed!"

The file is guaranteed to be closed at the end of the block -- even if you have a return, even if you raise an exception.

In order for with to make this guarantee, the expression (open() in the example) must be a context manager. The good news is that many python expressions are context managers, but not all.

According to a tutorial I found, MySQLdb.connect() is, in fact, a context manager.

This code:

conn = MySQLdb.connect(...)
with conn:
cur = conn.cursor()
cur.do_this()
cur.do_that()

will commit or rollback the sequence of commands as a single transaction. This means that you don't have to worry so much about exceptions or other unusual code paths -- the transaction will be dealt with no matter how you leave the code block.

Understanding the Python 'with' statement

I don't know why no one has mentioned this yet, because it's fundamental to the way with works. As with many language features in Python, with behind the scenes calls special methods, which are already defined for built-in Python objects and can be overridden by user-defined classes. In with's particular case (and context managers more generally), the methods are __enter__ and __exit__.

Remember that in Python everything is an object -- even literals. This is why you can do things like 'hello'[0]. Thus, it does not matter whether you use the file object directly as returned by open:

with open('filename.txt') as infile:
for line in infile:
print(line)

or store it first with a different name (for example to break up a long line):

the_file = open('filename' + some_var + '.txt')
with the_file as infile:
for line in infile:
print(line)

Because the end result is that the_file, infile, and the return value of open all point to the same object, and that's what with is calling the __enter__ and __exit__ methods on. The built-in file object's __exit__ method is what closes the file.

What is the python keyword with used for?

In python the with keyword is used when working with unmanaged resources (like file streams). It is similar to the using statement in VB.NET and C#. It allows you to ensure that a resource is "cleaned up" when the code that uses it finishes running, even if exceptions are thrown. It provides 'syntactic sugar' for try/finally blocks.

From Python Docs:

The with statement clarifies code that previously would use try...finally blocks to ensure that clean-up code is executed. In this section, I’ll discuss the statement as it will commonly be used. In the next section, I’ll examine the implementation details and show how to write objects for use with this statement.

The with statement is a control-flow structure whose basic structure is:

with expression [as variable]:
with-block

The expression is evaluated, and it should result in an object that supports the context management protocol (that is, has __enter__() and __exit__() methods).

Update fixed VB callout per Scott Wisniewski's comment. I was indeed confusing with with using.

What is the Python with statement used for?

There's a very nice explanation here. Basically, the with statement calls two special methods on the associated object. The __enter__ and __exit__ methods. The enter method returns the variable associated with the "with" statement. While the __exit__ method is called after the statement executes to handle any cleanup (such as closing a file pointer).

python with statement with class methods: AttributeError: __enter__

You would have to implement __enter__ and __exit__ on the classes' class.

class MyType(type):
def __enter__(cls):
print('call enter')
return cls

def __exit__(cls, *args):
print('call exit')

class Model(metaclass=MyType):
pass

Demo:

>>> with Model:
... pass
call enter
call exit

Model is an instance of MyType, the latter providing the magic methods.

Python's 'with' statement versus 'with .. as'

In general terms, the value assigned by the as part of a with statement is going to be whatever gets returned by the __enter__ method of the context manager.

What does a with statement do without the as?

On the Python side: Context managers run completely arbitrary code on enter and exit. Python doesn't force that code to do things "behind your back", but neither does it present them from doing so; so the implementation of the specific context manager at hand needs to be inspected to know why it's modifying behavior.

From a DearPyGui perspective: The C++ library that dpg backends into maintains a "container stack". Entering a container as a context manager ensures that it's on the top of your stack. When you create a new DearPyGui object that needs to be attached to a container, it refers to that stack, and attaches itself to the thing that is currently on top.



Related Topics



Leave a reply



Submit