Code block literals

Dave Benjamin ramen at lackingtalent.com
Thu Oct 9 19:09:00 CEST 2003


In article <840592e1.0310090454.4d95a170 at posting.google.com>, Hannu Kankaanp?? wrote:
> Dave Benjamin <dave at 3dex.com> wrote in message news:<u%0hb.113$_f.1 at news1.central.cox.net>...
>> For instance, I always thought this was a cooler alternative to the 
>> try/finally block to ensure that a file gets closed (I'll try not to 
>> mess up this time... ;) :
>> 
>> open('input.txt', { |f|
>>      do_something_with(f)
>>      do_something_else_with(f)
>> })
> 
> But being a function, it'd have the nasty property of a
> separate scope (yes, that can be nasty sometimes). I'd perhaps
> want to do
> 
> open('input.txt', { |f| data = f.read() })
> 
> But alas, 'data' would be local to the anonymous function and
> not usable outside.

Well, that's the same problem that lambda's got. I don't really have a
solution for that, other than the usual advice: "Use a namespace". =)

Dave

-- 
.:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g   l i f e   o u t   o f   t h e   c o n t a i n e r :




More information about the Python-list mailing list