[python-uk] TDD stuff in London, next two weeks (and list comprehension scoping)

Jonathan Hartley tartley at tartley.com
Sat May 31 12:13:30 CEST 2014


That's what I thought too, but:

$ python3
Python 3.4.0 (default, Apr 19 2014, 12:20:10)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> class Some(object):
...   tokens = ['a', 'b', 'c']
...   untokenized = [Some.tokens.index(a) for a in ['b']]
...
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 3, in Some
   File "<stdin>", line 3, in <listcomp>
NameError: name 'Some' is not defined


On 30/05/14 16:07, Sven Marnach wrote:
> On 30 May 2014 15:49, Harry Percival <harry.percival at gmail.com 
> <mailto:harry.percival at gmail.com>> wrote:
>
>     I had the problem outside of a class body, in a normal function...
>
> The particular problem mentioned in the StackOverflow quesiton you 
> linked only ever occurs inside class bodies.  They are the only 
> enclosing scopes that are skipped in name lookups.  You can still 
> access class attributes of the class by using ClassName.attribute 
> inside the list comprehension, like you would have to do to access 
> class attributes from inside methods.
>
> Cheers,
>     Sven
>
>
>
> _______________________________________________
> python-uk mailing list
> python-uk at python.org
> https://mail.python.org/mailman/listinfo/python-uk

-- 
Jonathan Hartley    tartley at tartley.com    http://tartley.com
Made of meat.       +44 7737 062 225       twitter/skype: tartley

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-uk/attachments/20140531/db68bc4a/attachment.html>


More information about the python-uk mailing list