<html>
<body>
At 10:06 PM 6/27/2008, Douglas Drumond wrote:<br><br>
<blockquote type=cite class=cite cite="">In a2() you do l1 += l2, ie, l1
= l1 + l2<br>
But if you don't have l1 defined yet, you can't add to l2<br>
It's like:<br>
<tt>def a2():</tt><br>
<tt> l1 = foo +
l2</tt><br><br>
<br>
<tt>UnboundLocalError: local variable 'foo' referenced before
assignment</tt><br><br>
It's because l1 (and foo at above example) is a local variable.<br>
a1's l1 is different from a2's l1.</blockquote><br>
Sorry to be dense, but how, in what way, is a1's l1 different from
a2's l1"? Both are <tt>[1,2,3]*100 .<br><br>
</tt>Dick<br><br>
<br>
<blockquote type=cite class=cite cite="">On Sat, Jun 28, 2008 at 01:39,
Dick Moores <<a href="mailto:rdm@rcblue.com">rdm@rcblue.com</a>>
wrote:<br>
<dl>
<dd>I'm puzzled by the below error msg. If I change the line in
<tt>a2()</tt> from<br><br>
<dd><tt>l1 = [1,2,3]*100<br><br>
</tt>
<dd>to<br><br>
<dd><tt>l1 = [1,2,3]<br><br>
</tt>
<dd>There is no problem. <br><br>
<dd>Why? And why isn't that line a problem for <tt>a1()</tt>?<br><br>
<dd>=========================================<br>
<dd><tt>def a1():<br>
<dd> return l1.extend(l2)<br>
<dd>if __name__=='__main__':<br>
<dd> l1 = [1,2,3]*100<br>
<dd> l2 = [4,5,6]<br>
<dd> from timeit import
Timer<br>
<dd> t =
Timer("a1()", "from __main__ import a1")<br>
<dd> t1 =
t.timeit(number=10)<br><br>
<dd> <br>
<dd>def a2():<br>
<dd> l1 += l2<br>
<dd>if __name__=='__main__':<br>
<dd> l1 = [1,2,3]*100<br>
<dd> l2 = [4,5,6]<br>
<dd> from timeit import
Timer<br>
<dd> t =
Timer("a2()", "from __main__ import a2")<br>
<dd> t2 =
t.timeit(number=10)<br><br>
<dd>print "t1:", t1<br>
<dd>print "t2:", t2<br>
<dd>print "t2/t1:", t2/t1<br><br>
<dd>Error msg:<br>
<dd>E:\PythonWork>timing_2_stupidsV2.py<br>
<dd>Traceback (most recent call last):<br>
<dd> File "E:\PythonWork\timing_2_stupidsV2.py", line 21,
in <module><br>
<dd> t2 = t.timeit(number=10)<br>
<dd> File "E:\Python25\lib\timeit.py", line 161, in
timeit<br>
<dd> timing = self.inner(it, self.timer)<br>
<dd> File "<timeit-src>", line 6, in inner<br>
<dd> File "E:\PythonWork\timing_2_stupidsV2.py", line 15,
in a2<br>
<dd> l1 += l2<br>
<dd>UnboundLocalError: local variable 'l1' referenced before
assignment<br>
</tt>
<dd>===================================================<br><br>
<dd>Thanks,<br>
<font color="#888888"><br>
<dd>Dick Moores<br>
</font><br>
<dd>_______________________________________________<br>
<dd>Tutor maillist -
<a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
<dd>
<a href="http://mail.python.org/mailman/listinfo/tutor" eudora="autourl">
http://mail.python.org/mailman/listinfo/tutor</a><br><br>
</dl><br><br>
_______________________________________________<br>
Tutor maillist - Tutor@python.org<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" eudora="autourl">
http://mail.python.org/mailman/listinfo/tutor</a></blockquote></body>
</html>