All pastes #358572 Raw Edit

Untitled

public text v1 · immutable
·published 2007-02-16 09:31 UTC
rendered paste body
using System;

class foo
{
	delegate void G (int x);

	static void Main ()
	{
		Works ();
		Fails ();
	}

	static void Works ()
	{
		int i = 0;

		G g = delegate (int x) { Console.WriteLine (i + " " + x); };

		g (1);
		g (2);
		g (3);
		g (4);

		Console.WriteLine (i);
	}

	static void Fails ()
	{
		int i = 0;

		G g = delegate (int x) { i += x; };

		g (1);
		g (2);
		g (3);
		g (4);

		Console.WriteLine (i);
	}
}

0 1
0 2
0 3
0 4
0
10


#!/usr/bin/python

def works():
    i = 0
    def g(x):
        print i, x
    g(1)
    g(2)
    g(3)
    g(4)
    print i

def fails():
    i = 0
    def g(x):
        i += x
    g(1)
    g(2)
    g(3)
    g(4)
    print i

works()
fails()

alp@rover:~/tmp$ python foo.py 
0 1
0 2
0 3
0 4
0
Traceback (most recent call last):
  File "foo.py", line 24, in <module>
    fails()
  File "foo.py", line 17, in fails
    g(1)
  File "foo.py", line 16, in g
    i += x
UnboundLocalError: local variable 'i' referenced before assignment