nested classes

Alex Martelli aleaxit at yahoo.com
Mon Jun 18 12:05:43 EDT 2001


"Stefan Seefeld" <seefeld at sympatico.ca> wrote in message
news:3B2E2D3A.75F329D at sympatico.ca...
> hi there,
>
> I'd like to provide a hierarchy of config data by means of
> nested classes and static variables, such as:
>
> class A:
>     prefix='usr/local'
>     class B:
> datadir = A.prefix + '/share'
> ...
>
> however, I get a NameError: name 'A' is not defined

class-object A does not yet exist (and thus in particular
its name is not yet bound to it) until its classbody has
_finished_ executing.

You could do something like:

class A:
    prefix = '/usr/local'
    class B:
        pass    # or whatever...
    B.datadir = prefix + '/share'


Alex






More information about the Python-list mailing list