Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Possible Duplicate:
Short Description of Python Scoping Rules Short Description of Python Scoping Rules

I wrote two simple functions:

# coding: utf-8
def test():
 var = 1 
 def print_var():
 print var 
 print_var()
 print var 
 
test()
# 1
# 1
def test1():
 var = 2 
 def print_var():
 print var 
 var = 3 
 print_var()
 print var 
 
test1()
# raise Exception

In comparison, test1() assigns value after print var, then raise an Exception: UnboundLocalError: local variable 'var' referenced before assignment, I think the moment I call inner print var, var has a value of 2, am I wrong?

Possible Duplicate:
Short Description of Python Scoping Rules

I wrote two simple functions:

# coding: utf-8
def test():
 var = 1 
 def print_var():
 print var 
 print_var()
 print var 
 
test()
# 1
# 1
def test1():
 var = 2 
 def print_var():
 print var 
 var = 3 
 print_var()
 print var 
 
test1()
# raise Exception

In comparison, test1() assigns value after print var, then raise an Exception: UnboundLocalError: local variable 'var' referenced before assignment, I think the moment I call inner print var, var has a value of 2, am I wrong?

Possible Duplicate:
Short Description of Python Scoping Rules

I wrote two simple functions:

# coding: utf-8
def test():
 var = 1 
 def print_var():
 print var 
 print_var()
 print var 
 
test()
# 1
# 1
def test1():
 var = 2 
 def print_var():
 print var 
 var = 3 
 print_var()
 print var 
 
test1()
# raise Exception

In comparison, test1() assigns value after print var, then raise an Exception: UnboundLocalError: local variable 'var' referenced before assignment, I think the moment I call inner print var, var has a value of 2, am I wrong?

insert duplicate link
Source Link

Possible Duplicate:
Short Description of Python Scoping Rules

I wrote two simple functions:

# coding: utf-8
def test():
 var = 1 
 def print_var():
 print var 
 print_var()
 print var 
 
test()
# 1
# 1
def test1():
 var = 2 
 def print_var():
 print var 
 var = 3 
 print_var()
 print var 
 
test1()
# raise Exception

In comparison, test1() assigns value after print var, then raise an Exception: UnboundLocalError: local variable 'var' referenced before assignment, I think the moment I call inner print var, var has a value of 2, am I wrong?

I wrote two simple functions:

# coding: utf-8
def test():
 var = 1 
 def print_var():
 print var 
 print_var()
 print var 
 
test()
# 1
# 1
def test1():
 var = 2 
 def print_var():
 print var 
 var = 3 
 print_var()
 print var 
 
test1()
# raise Exception

In comparison, test1() assigns value after print var, then raise an Exception: UnboundLocalError: local variable 'var' referenced before assignment, I think the moment I call inner print var, var has a value of 2, am I wrong?

Possible Duplicate:
Short Description of Python Scoping Rules

I wrote two simple functions:

# coding: utf-8
def test():
 var = 1 
 def print_var():
 print var 
 print_var()
 print var 
 
test()
# 1
# 1
def test1():
 var = 2 
 def print_var():
 print var 
 var = 3 
 print_var()
 print var 
 
test1()
# raise Exception

In comparison, test1() assigns value after print var, then raise an Exception: UnboundLocalError: local variable 'var' referenced before assignment, I think the moment I call inner print var, var has a value of 2, am I wrong?

Post Closed as "exact duplicate" by Community Bot, sloth, David Robinson, g.d.d.c, Martijn Pieters
Source Link
iMom0
  • 13k
  • 3
  • 52
  • 61

Doubts about python variable scope

I wrote two simple functions:

# coding: utf-8
def test():
 var = 1 
 def print_var():
 print var 
 print_var()
 print var 
 
test()
# 1
# 1
def test1():
 var = 2 
 def print_var():
 print var 
 var = 3 
 print_var()
 print var 
 
test1()
# raise Exception

In comparison, test1() assigns value after print var, then raise an Exception: UnboundLocalError: local variable 'var' referenced before assignment, I think the moment I call inner print var, var has a value of 2, am I wrong?

lang-py

AltStyle によって変換されたページ (->オリジナル) /