[ create a new paste ] login | about

Link: http://codepad.org/FXmgt964    [ raw code | output | fork ]

Python, pasted on May 22:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
x = 1

def one():
  "This is perfectly fine."
  y = x
  return y

def two():
  "This too!"
  x = 2
  return x

def three():
  "Python creates a new local x, and assigns it x+2, but x has no value yet."
  x = x + 2
  return x

print one()
print two()
print three()


Output:
1
2
3
4
5
6
7
8
1
2
Traceback (most recent call last):
  Line 20, in <module>
    print three()
  Line 15, in three
    x = x + 2
UnboundLocalError: local variable 'x' referenced before assignment


Create a new paste based on this one


Comments: