Is this a bug, or is it me?

Neil Cerutti mr.cerutti at gmail.com
Thu Jan 17 10:23:05 EST 2008


On Jan 17, 2008 10:05 AM,  <cptnwillard at gmail.com> wrote:
> Hello all,
> For some reason, the following does not work :
>
>
> class C:
>     TYPES = [None]
>     DICT = {}
>     for Type in TYPES:
>         DICT.update((E,Type) for E in [1])
>
> >>> NameError: global name 'Type' is not defined
>
>
> What do you think? Is this a bug?

You cannot access a class's class variables in it's class-statement
scope, since the name of the type is not bound until after the class
statement is completed.

Try:

class C:
  TYPES = [None]
  DICT = {}

for Type in C.TYPES:
  C.DICT.update((E, Type) for E in [1])

-- 
Neil Cerutti <mr.cerutti+python at gmail.com>



More information about the Python-list mailing list