On PEP 312: simple implicit lambda
Andrew Bennetts
andrew-pythonlist at puzzling.org
Sun Feb 16 10:32:04 EST 2003
On Sun, Feb 16, 2003 at 04:47:11PM +0200, Christos TZOTZIOY Georgiou wrote:
>
> Take for example:
>
> data = file_object.read(4096)
> while data:
> # process here
> data = file_object.read(4096)
>
> (In this simple case, the need to avoid duplication is not that strong,
> so it is a bad example, but it's just an idea that popped up in my mind
> a few minutes ago; I won't stand up to it if proven to be stupid :)
> There are cases with if...elif...else constructs where duplicate code is
> used too, so if needed, I will find more examples.)
Why not simply:
data = 'dummy'
while data:
data = file_object.read(4096)
# process here
This avoids the duplication quite neatly.
> What I would do in such a case, would be the following:
>
> <read_more_data>:
> data = file_object.read(4096)
> while data:
> # process here
> <read_more_data>
You can do this already:
def read_more_data():
return file_object.read(4096)
data = read_more_data()
while data:
# process here
data = read_more_data()
Slightly different, but doesn't require any new features with gratuitous
punctuation. I still prefer my earlier alternative.
A much less radical solution than yours would be if Python supported full
closures, i.e. being able to bind variables in outer scopes from a nested
function, then you could do:
def read_more_data():
outer data # By analogy with the global statement
data = file_object.read(4096)
read_more_data()
while data:
# process here
read_more_data()
Of course, my fictitious "outer" statement would be problematic with
multiple levels of function nesting... I'm not sure that "outer outer outer foo"
is a construct people want to see, although I suspect it'd be more popular
than your proposal.
Do you have any other examples where you think this feature would be useful?
-Andrew.
More information about the Python-list
mailing list