#!/usr/bin/env ruby # This code comes from http://ruby-it.org/pages/Closure+in+python # Check the page for copyright notice and explanations >>> def esponenziale(n): ... lambda a: n=n*n ... File "", line 2 SyntaxError: can't assign to lambda >>> def square_generator(num): ... def f(): ... return num * num ... return f ... >>> square2=square_generator(2) >>> square2() 4 >>> def exp(num): ... def f(): ... num= num * num ... return num ... return f ... >>> potenze=exp(2) >>> potenze() Traceback (most recent call last): File "", line 1, in ? File "", line 3, in f UnboundLocalError: local variable 'num' referenced before >>> def exp(num): ... ary=[num] ... def f(): ... ary[0]= ary[0]**2 ... return ary[0] ... return f ... >>> square=exp(2) >>> square() 4 >>> square() 16