<div dir="ltr">ah.  I think i figured it out -- it's actually just a weird py.test thing. something to do with its assert-introspection-voodoo...<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 30 May 2014 16:16, Roger Gammans <span dir="ltr"><<a href="mailto:roger@gammascience.co.uk" target="_blank">roger@gammascience.co.uk</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Fri, 2014-05-30 at 16:07 +0100, Sven Marnach wrote:<br>
> On 30 May 2014 15:49, Harry Percival <<a href="mailto:harry.percival@gmail.com">harry.percival@gmail.com</a>> wrote:<br>
>         I had the problem outside of a class body, in a normal<br>
>         function...<br>
><br>
><br>
> The particular problem mentioned in the StackOverflow quesiton you<br>
> linked only ever occurs inside class bodies.  They are the only<br>
> enclosing scopes that are skipped in name lookups.  You can still<br>
> access class attributes of the class by using ClassName.attribute<br>
> inside the list comprehension, like you would have to do to access<br>
> class attributes from inside methods.<br>
<br>
</div></div>Hmm. It quite odd, it appears to only affect the evaluation part of the<br>
list-comprehension too. Notice the way that a naked reference to a works<br>
as the argument to range() , but not when used in the expr part of the<br>
list comprehension.<br>
<br>
$ python3.3<br>
Python 3.3.3 (default, Dec  8 2013, 14:51:59)<br>
[GCC 4.8.2] on linux<br>
Type "help", "copyright", "credits" or "license" for more information.<br>
>>> class X:<br>
...    a = 3<br>
...    b = [ y for y in range(a) ]<br>
...<br>
>>> X.b<br>
[0, 1, 2]<br>
>>> class X:<br>
...    a = 3<br>
...    b = [ y*a for y in range(a) ]<br>
...<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
  File "<stdin>", line 3, in X<br>
  File "<stdin>", line 3, in <listcomp><br>
NameError: global name 'a' is not defined<br>
>>> class X:<br>
...    a = 3<br>
...    b = [ y*X.a for y in range(a) ]<br>
...<br>
>>> X.b<br>
[0, 3, 6]<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Roger Gammans <<a href="mailto:roger@gammascience.co.uk">roger@gammascience.co.uk</a>><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>------------------------------<br>Harry J.W. Percival<br>------------------------------<br>Twitter: @hjwp<br>Mobile:  +44 (0) 78877 02511<br>Skype:         harry.percival
</div>