Python scope problems only when _assigning_ to a variable
Posted
by wallacoloo
on Stack Overflow
See other posts from Stack Overflow
or by wallacoloo
Published on 2010-05-22T04:16:14Z
Indexed on
2010/05/22
4:20 UTC
Read the original article
Hit count: 239
So I'm having a very strange error right now. I found where it happens, and here's the simplest code that can reproduce it.
def parse_ops(str_in):
c_type = "operator"
def c_dat_check_type(t):
print c_type
#c_type = t
c_dat_check_type("number")
>>> parse_ops("12+a*2.5")
If you run it as-is, it prints "operator". But if you uncomment that line, it gives an error:
Traceback (most recent call last):
File "<pyshell#212>", line 1, in <module>
parse_ops("12+a*2.5")
File "<pyshell#211>", line 7, in parse_ops
c_dat_check_type("number")
File "<pyshell#211>", line 4, in c_dat_check_type
print c_type
UnboundLocalError: local variable 'c_type' referenced before assignment
Notice the error occurs on the line that worked just fine before.
Any ideas what causes this and how I can fix this? I'm using Python 2.6.1.
© Stack Overflow or respective owner