I find that I am often writing code in the following pattern: foo = MyContextManager(*args) for bar in my_iter: with foo: # do stuff I think it would be much cleaner to be able to write: for bar in my_iter with MyContextManager(*args): # do stuff -Mark
On Aug 2, 2011, at 3:11 PM, Mark McDuff wrote:
I find that I am often writing code in the following pattern:
foo = MyContextManager(*args) for bar in my_iter: with foo: # do stuff
I think it would be much cleaner to be able to write:
for bar in my_iter with MyContextManager(*args): # do stuff
It's not clear to me whether that means
for bar in my_iter: with MyContextManager(*args): # do stuff
or
with MyContextManager(*args): for bar in my_iter: # do stuff
-0.
On Wed, Aug 3, 2011 at 11:11 AM, Mark McDuff <mmcduff@gmail.com> wrote:
I find that I am often writing code in the following pattern:
foo = MyContextManager(*args) for bar in my_iter: with foo: # do stuff
I think it would be much cleaner to be able to write:
for bar in my_iter with MyContextManager(*args): # do stuff
I'm not sure why you think putting the context manager way over on the right hand side is an improvement, but no, merging arbitrary statements is never going to happen (even the comprehension inspired merging of for and if statements has been explicitly rejected many many times). As Carl notes, the ambiguity of the propose syntax is also not good - it is unclear whether the context manager is recreated on each pass around the loop, reused on each pass, or applied once to cover the entire loop operation. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
-1 On Wed, Aug 3, 2011 at 11:56 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
On Wed, Aug 3, 2011 at 11:11 AM, Mark McDuff <mmcduff@gmail.com> wrote:
I find that I am often writing code in the following pattern:
foo = MyContextManager(*args) for bar in my_iter: with foo: # do stuff
I think it would be much cleaner to be able to write:
for bar in my_iter with MyContextManager(*args): # do stuff
I'm not sure why you think putting the context manager way over on the right hand side is an improvement, but no, merging arbitrary statements is never going to happen (even the comprehension inspired merging of for and if statements has been explicitly rejected many many times).
As Carl notes, the ambiguity of the propose syntax is also not good - it is unclear whether the context manager is recreated on each pass around the loop, reused on each pass, or applied once to cover the entire loop operation.
Cheers, Nick.
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
On Tue, Aug 2, 2011 at 6:11 PM, Mark McDuff <mmcduff@gmail.com> wrote:
I find that I am often writing code in the following pattern:
foo = MyContextManager(*args) for bar in my_iter: with foo: # do stuff
I think it would be much cleaner to be able to write:
for bar in my_iter with MyContextManager(*args): # do stuff
Some have similarly suggested: for x in y if foo(x): # do stuff Where would it end? What if someone wants: for bar in foo with context if baz: # stuff ? Even just the Cartesian product of all Python's control structures with themselves quickly becomes unwieldy. Down this path lies Perl (particularly its control-structures-as-statement-suffixes feature). The simplicity and regularity gained is worth having to suffer 1 additional level of indentation now and then; refactor your code if the number of levels of indentation in it is becoming problematic. Cheers, Chris -- http://rebertia.com
On Tue, Aug 2, 2011 at 6:11 PM, Mark McDuff <mmcduff@gmail.com> wrote:
I find that I am often writing code in the following pattern:
foo = MyContextManager(*args) for bar in my_iter: with foo: # do stuff
I think it would be much cleaner to be able to write:
for bar in my_iter with MyContextManager(*args): # do stuff
The parts of the for statement have *no connection at all* to the parts of the with statement. They're just stuck together which doesn't make much sense to me. When I read the subject of the original mail I immediately thought of this case: with open(foo) as _: for line in _: # stuff which would at least make some sense if we could splice these together as with line in open(foo): #stuff But no matter how common this might be, I have to agree with: On Tue, Aug 2, 2011 at 9:32 PM, Chris Rebert <pyideas@rebertia.com> wrote:
... Down this path lies Perl ...
For every combination like this, there's another one just past it on the road to Perl. --- Bruce Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
Chris Rebert wrote:
Where would it end? What if someone wants: for bar in foo with context if baz: # stuff
With a slight relaxation of the rules concerning statements on a single line, one could write for bar in foo: with context: if baz: # stuff Not that I'd really advocate that, but it might help to shut up the people who keep requesting this sort of thing. -- Greg
participants (7)
-
Bruce Leban
-
Carl Matthew Johnson
-
Chris Rebert
-
Greg Ewing
-
Mark McDuff
-
Matt Joiner
-
Nick Coghlan