[Python-ideas] Object grabbing

Koos Zevenhoven k7hoven at gmail.com
Mon May 2 17:34:15 EDT 2016

On Mon, May 2, 2016 at 11:41 PM, MRAB <python at mrabarnett.plus.com> wrote:
> On 2016-05-02 14:57, Koos Zevenhoven wrote:
>> On Mon, May 2, 2016 at 3:44 PM, Alexander Walters wrote:
>>> with open(foo), open(bar):
>>>     .write(baz)  # does what?
>> Hopefully it would raise an exception, even though, in the spirit of
>> May 1st, it would pick one of the two files at random ;-).
> Anyway, the exception (or warning?) could be raised at compile-time if the
> leading-dot form was used in a 'with' that had more than one expression.

Exactly. A SyntaxError perhaps?

So, if a single-expression with-statement would allow dot-attribute
syntax for referring to attributes of the value, like in my previous

with open(filename, 'w'):
    #do stuff
    #more stuff

Then, if one introduces something roughly as follows:

import contextlib
def implicit(obj):
    yield obj

It would also allow one to do things like:

with implicit(food):
    meal = .spam + .eggs + .cheese

# meal = food.spam + food.eggs + food.cheese

with implicit(self):
    .foo = foo  # self.foo = foo
    .bar = bar  # self.bar = bar

This would be very explicit about what is implicit :).

This way, the required addition to the with statement would indeed not
change the meaning of 'with' (or mix it with different meanings). The
only(?) addition would be .attr access to attributes of the object
"yielded" by the context manager.

-- Koos

More information about the Python-ideas mailing list