Python Feature Request: Add the "using" keyword which works like "with" in Visual Basic

BJörn Lindqvist bjourne at gmail.com
Sat Apr 14 09:58:44 EDT 2007


Your idea isn't new and has already been discussed lots of time
before. It was once planned to be implemented in py3k, but no longer
is.

One of the problems is that with a "using" statement, you always have
to decide whether your code repeats some prefix enough times to use a
"using" statement. Should you write:

self.quit.action = self.bar
self.quit.name = "End it"

or should it be:

using self.quit:
    .action = self.bar
    .name = "End it"

? Not having to bother with petty things like that is an advantage.
Javascript has with-statements that are equivalent to your
using-statements but from what I've seen most programmers avoid them.
They don't increase readability one bit.

You already can emulate the using statement like this:

def using(obj, **kw):
    for key, val in kw.items():
        setattr(obj, key, val)

using(self.quit,
    action = self.bar,
    name = "End it")

But I have never seen anyone do that, which I think, is a sign that
nobody wants the feature.

-- 
mvh Björn



More information about the Python-list mailing list