Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread Ethan Furman
On 01/12/2015 08:49 PM, Steven D'Aprano wrote: > On Mon, 12 Jan 2015 12:40:13 -0800, Ethan Furman wrote: >> >> [...] class name lookup skips nonlocal namespaces. > > Actually, no it doesn't. > [...] > The "problem" is that *functions* lookup don't include the class body in > their scope. Ah, t

Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread Steven D'Aprano
On Mon, 12 Jan 2015 12:40:13 -0800, Ethan Furman wrote: > On 01/12/2015 12:25 PM, John Ladasky wrote: >> d = {0:"a", 1:"b", 2:"c", 3:"d"} >> e = [d[x] for x in (0,2)] >> >> class Foo: >> f = {0:"a", 1:"b", 2:"c", 3:"d"} >> print(f) >> g = [f[x] for x in (0,2)] > > In Foo 'f' is part

Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 7:25 AM, John Ladasky wrote: > When I am working inside the class namespace, the print function call on line > 8 recognizes the name f and prints the dictionary bound to that name. > > However, the LIST COMPREHENSION defined inside the class namespace generates > a NameEr

Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread John Ladasky
On Monday, January 12, 2015 at 12:41:30 PM UTC-8, Ethan Furman wrote: > In Foo 'f' is part of an unnamed namespace; the list comp 'g' has its own > namespace, effectively making be a nonlocal; > class name lookup skips nonlocal namespaces. > > Workaround: use an actual for loop. Thanks, Ethan.

Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread John Ladasky
Following up to myself: I finally did the right keyword search, and found a relevant article: http://stackoverflow.com/questions/13905741/accessing-class-variables-from-a-list-comprehension-in-the-class-definition Maybe I HAVE tried to define a list comprehension inside a class definition befor

Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread Ethan Furman
On 01/12/2015 12:25 PM, John Ladasky wrote: > d = {0:"a", 1:"b", 2:"c", 3:"d"} > e = [d[x] for x in (0,2)] > > class Foo: > f = {0:"a", 1:"b", 2:"c", 3:"d"} > print(f) > g = [f[x] for x in (0,2)] In Foo 'f' is part of an unnamed namespace; the list comp 'g' has its own namespace, eff