nested classes
gbreed at cix.compulink.co.uk
gbreed at cix.compulink.co.uk
Mon Jun 18 13:08:15 EDT 2001
In article <3B2E2D3A.75F329D at sympatico.ca>, seefeld at sympatico.ca
(Stefan Seefeld) wrote:
> 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
See the earlier message "getting a reference to a class inside
its definition" and the flood of no good ideas at all that
followed it.
> What am I doing wrong ? The above is possibly a bit driven
> by my C++ programming style. What is the python way of doing
> this ?
You can get that to work in C++? It looks strange to me, I'm
sure you can't be trying to do what you think you're trying to
do. This is the nearest I can think of that works:
>>> class A:
... prefix = 'usr/local'
...
>>> class B(A):
... datadir = A.prefix+'/share'
...
>>> A.prefix
'usr/local'
>>> B.datadir
'usr/local/share'
You may also like to take a look at os.path.join
>>> import os.path
>>> class A:
... prefix = os.path.join('usr','local')
...
>>> class B(A):
... datadir = os.path.join(A.prefix, 'share')
...
>>> print A.prefix
usr\local
>>> print B.datadir
usr\local\share
>>> print B.prefix
usr\local
It'll give the right results on a UNIX system, and stop double
or omitted slashes.
Graham
More information about the Python-list
mailing list