Closures / Blocks in Python

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


On 2007-07-24, treble54 <treble54 at gmail.com> 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.

Ruby's

  some_list.each do |item|
  puts item
  end

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