Method in Methods and Variables
Thomas Guettler
zopestoller at thomas-guettler.de
Mon Feb 3 09:11:50 EST 2003
Hi!
The following python code behaves strange:
import re
def test(my_int, my_list):
content='"a" "b" "c"'
def sub(matchObject):
my_string=matchObject.group(1)
print my_int
my_int+=1 # (*) If I uncomment this line it works
print my_list
return "-%s:%s-" % (my_string, my_int)
content=re.sub(r'"(.*?)"',
sub, content)
print content
test(12, [14])
-->
Traceback (most recent call last):
File "sub-test.py", line 13, in ?
test(12, [14])
File "sub-test.py", line 11, in test
sub, content)
File "/usr/lib/python2.2/sre.py", line 143, in sub
return _compile(pattern, 0).sub(repl, string, count)
File "sub-test.py", line 6, in sub
print my_int
UnboundLocalError: local variable 'my_int' referenced before assignment
If I uncomment the line (*) it works:
===> python sub-test.py
12
[14]
12
[14]
12
[14]
-a:12- -b:12- -c:12-
It is strange, that I can read my_int but I can't set it. I think
putting the integer into a list should work.
thomas
More information about the Python-list
mailing list