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

Michael Foord fuzzyman at voidspace.org.uk
Sun Jun 1 16:54:52 CEST 2014



Sent from my iPhone

> On 1 Jun 2014, at 01:12, John Pinner <funthyme at gmail.com> wrote:
> 
> Hello All,
> 
>> On 31 May 2014 23:02, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
>> 
>>> On 31 May 2014, at 11:13, Jonathan Hartley <tartley at 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.
> 
> or maybe (correct me if I'm wrong) until the interpreter has finished
> compiling the class (which does not happen until the end of the class
> definition has been reached).

No, it's at execution time. Compilation does not execute code and so does not bind names. Class bodies are executed to generate the class.

Michael


> 
>> You should just be able to access the name "tokens" from inside the class body though:
> <snips>
> 
> All the best,
> 
> John
> --
> _______________________________________________
> python-uk mailing list
> python-uk at python.org
> https://mail.python.org/mailman/listinfo/python-uk


More information about the python-uk mailing list