On 31 May 2014, at 11:13, Jonathan Hartley <tart...@tartley.com> wrote:

> 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
> 

The name Some is not bound until the class body has finished executing. You 
should just be able to access the name "tokens" from inside the class body 
though:

 python3
Python 3.3.4 (v3.3.4:7ff62415e426, Feb  9 2014, 00:29:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Some:
...     tokens = [1, 2, 3]
...     thing = [token ** 2 for token in tokens]
... 
>>> 

Michael

> 
> On 30/05/14 16:07, Sven Marnach wrote:
>> On 30 May 2014 15:49, Harry Percival <harry.perci...@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@python.org
>> https://mail.python.org/mailman/listinfo/python-uk
> 
> -- 
> Jonathan Hartley    
> tart...@tartley.com    http://tartley.com
> 
> Made of meat.       +44 7737 062 225       twitter/skype: tartley
> 
> 
> _______________________________________________
> python-uk mailing list
> python-uk@python.org
> https://mail.python.org/mailman/listinfo/python-uk


--
http://www.voidspace.org.uk/


May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing 
http://www.sqlite.org/different.html





_______________________________________________
python-uk mailing list
python-uk@python.org
https://mail.python.org/mailman/listinfo/python-uk

Reply via email to