High Order Messages in Python

Kent Johnson kent37 at tds.net
Mon Oct 24 06:17:18 EDT 2005


bonono at gmail.com wrote:
> counting that out(regardless whether it is (dis)advantage or not), what
> else a block can do but not a named function ?

My limited understanding is that the advantage is
- simpler syntax
- high level of integration into the standard library (*many* methods that take closure arguments). Blocks are used not just for iteration but for the kinds of things shown in the examples to PEP 343 http://www.python.org/peps/pep-0343.html

For example to open a file and read from it uses two closures, one to wrap a block with the file open/close, one to iterate lines (from the pickaxe book):

File.open("testfile") do |file|
  file.each_line { |line| puts line }
end

Kent

> 
> Alex Martelli wrote:
> 
>>bonono at gmail.com <bonono at gmail.com> wrote:
>>
>>
>>>could someone enlighten me what is the advantage of block over named
>>>function ?
>>>
>>>One thing that I can see a difference may be lexical scope ?
>>
>>"Yes, but" -- according to the latest Ruby book, the "mixed lexical
>>scope" of blocks is a highly controversial notion in the Ruby community;
>>so I wouldn't necessarily count it as an _advantage_ of Ruby blocks...
>>
>>
>>Alex
> 
> 



More information about the Python-list mailing list