Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"with" statement in python, why must the "as" section be a single object

With Python one can say:

a,b,c=something_that_returns_a_3_tuple()

But a with statement like:

class thing(object):
    def __enter__(self):
        return (1,2,3)

    def __exit__(self,a,b,c):
        pass

with thing() as a,b,c:
    print a
    print b 
    print c

will not work work

One must have:

class thing(object):
    def __enter__(self):
        return (1,2,3)

    def __exit__(self,a,b,c):
        pass

with thing() as (a,b,c):
    print a
    print b 
    print c

I can't see a practical problem with allowing the first form, by this I mean implementation or logical, the parser shouldn't have a problem with commas (it wont be ambiguous) and I see no logical reason why not.

http://docs.python.org/release/2.5/whatsnew/pep-343.html

and http://www.python.org/dev/peps/pep-0343/ suggests this is just syntatic sugar

like image 777
Alec Teal Avatar asked Dec 14 '13 21:12

Alec Teal


People also ask

How does the with statement work in Python?

The with statement in Python is used for resource management and exception handling. You'd most likely find it when working with file streams. For example, the statement ensures that the file stream process doesn't block other processes if an exception is raised, but terminates properly.

What is the purpose of the with as argument in Python?

with statement in Python is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Observe the following code example on how the use of with statement makes code cleaner.

How does the with statement work?

The with statement stores the Saved object in a temporary, hidden variable, since it'll be needed later. (Actually, it only stores the bound __exit__ method, but that's a detail.) The with statement calls __enter__ on the Saved object, giving the context manager a chance to do its job.

What if we dont use with statement in Python?

You get no error, so this is a valid statement, and you can write it on its own line in a Python script, and it won't raise an error. Since you're not storing it in a variable, the result (4) will be discarded, and the statement will have no effect. The same happens with the statement f.


1 Answers

You cannot use a tuple target without parenthesis in the syntax, because otherwise you cannot distinguish between multiple context managers:

with contextmanager1 as target1, contextmanager2 as target2:

See the comma there? Because the grammar allows for specifying multiple context managers, the compiler cannot distinguish between that form and multiple targets specified with just commas.

Ah, you say, but there is an as target2 part there, can't that be used to distinguish the forms? Then remember, that the target is optional. The following is perfectly legal syntax too:

with contextmanager1 as target1, contextmanager2:

Python knows the part after the comma is another context manager there, because it disallowed for multiple targets without parenthesis, removing all ambiguity.

Support for multiple context managers was introduced after PEP 343 was implemented, in Python 2.7 and 3.1. However, the PEP did anticipate the possibility:

This restriction makes a future extension possible of the syntax to have multiple comma-separated resources, each with its own optional as-clause.

like image 184
Martijn Pieters Avatar answered Oct 02 '22 13:10

Martijn Pieters