[Python-ideas] adding an __exec__ method to context managers?
Carl Johnson
cmjohnson.mailinglist at gmail.com
Tue Oct 13 09:00:30 CEST 2009
Ben Finney:
> What is insufficiently readable about::
>
> def foo(spam):
> process_wibble(spam)
> process_wobble(spam)
> return process_warble(spam)
>
> bar(foo)
The problem with that is in 3 or 4 months someone will come back to
Python-ideas with another theory of how to replace it.
More seriously, it puts things out of order. If you had to write for-loops as
def loop(item):
process(item), etc.
for(loop, iterator)
it would be patently obvious that the for(loop, iterator) belongs at
the top, not the bottom, so that you know *what* is being iterated
before you find out *how* it's being iterated. For that matter, Python
has changed the perfectly sensible:
def method(cls):
stuff()
method = classmethod(method)
to
@classmethod
def method(cls):
stuff()
Why? Because it's more readable to have the decorator up top, so you
know what kind of function/method to expect.
So, in this particular case, I think it's more readable to have the
conditions at the top instead of the bottom.
It's very natural when something that starts as
for i in range(100):
dostuff1(i) # thread safe function
dostuff2(i) # not thread safe function
becomes
with parallelize(range(100)) as i:
dostuff1(i) # thread safe function
dostuff2(i) # not thread safe function
(or some other way of writing the condition at the top instead of,
such as a decorator etc.) instead of
def f(i):
dostuff1(i) # thread safe function
dostuff2(i) # not thread safe function
parallelize(f, range(100))
with the loop condition at the bottom.
For whatever reason, people find putting the conditions out of order
onerous, and Python-ideas won't be free of periodic interruptions
until there someway to conditions into their mental order.
— Carl
More information about the Python-ideas
mailing list