<html>
<body>
I'm puzzled by the below error msg. If I change the line in <tt>a2()</tt>
from<br><br>
<tt>l1 = [1,2,3]*100<br><br>
</tt>to<br><br>
<tt>l1 = [1,2,3]<br><br>
</tt>There is no problem. <br><br>
Why? And why isn't that line a problem for <tt>a1()</tt>?<br><br>
=========================================<br>
<tt>def a1():<br>
<x-tab> </x-tab>return
l1.extend(l2)<br>
if __name__=='__main__':<br>
<x-tab> </x-tab>l1 =
[1,2,3]*100<br>
<x-tab> </x-tab>l2 =
[4,5,6]<br>
<x-tab> </x-tab>from
timeit import Timer<br>
<x-tab> </x-tab>t =
Timer("a1()", "from __main__ import a1")<br>
<x-tab> </x-tab>t1 =
t.timeit(number=10)<br><br>
<x-tab> </x-tab><br>
def a2():<br>
<x-tab> </x-tab>l1 +=
l2<br>
if __name__=='__main__':<br>
<x-tab> </x-tab>l1 =
[1,2,3]*100<br>
<x-tab> </x-tab>l2 =
[4,5,6]<br>
<x-tab> </x-tab>from
timeit import Timer<br>
<x-tab> </x-tab>t =
Timer("a2()", "from __main__ import a2")<br>
<x-tab> </x-tab>t2 =
t.timeit(number=10)<br><br>
print "t1:", t1<br>
print "t2:", t2<br>
print "t2/t1:", t2/t1<br><br>
Error msg:<br>
E:\PythonWork>timing_2_stupidsV2.py<br>
Traceback (most recent call last):<br>
File "E:\PythonWork\timing_2_stupidsV2.py", line 21, in
<module><br>
t2 = t.timeit(number=10)<br>
File "E:\Python25\lib\timeit.py", line 161, in
timeit<br>
timing = self.inner(it, self.timer)<br>
File "<timeit-src>", line 6, in inner<br>
File "E:\PythonWork\timing_2_stupidsV2.py", line 15, in
a2<br>
l1 += l2<br>
UnboundLocalError: local variable 'l1' referenced before assignment<br>
</tt>===================================================<br><br>
Thanks,<br><br>
Dick Moores</body>
</html>