Closures / Blocks in Python

Neil Cerutti horpner at
Tue Jul 24 17:30:12 CEST 2007

On 2007-07-24, treble54 <treble54 at> wrote:
> Does anyone know a way to use closures or blocks in python like
> those used in Ruby? Particularly those used in the { } braces.

Python's nameless functions are week. So it supports iterators
and generators using protocols, comprehensions and a few simple
statements, rather than promoting the use of nameless functions.


  some_list.each do |item|
  puts item

if I understood it correctly, In Python would be:

  for item in some_list:
    print item

That works for any object that supports the iterator protocol.

Neil Cerutti

More information about the Python-list mailing list