nested classes

Stefan Seefeld seefeld at sympatico.ca
Mon Jun 18 14:23:33 EDT 2001


gbreed at cix.compulink.co.uk wrote:

> > 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:

uh, what do *you* think that I'm trying to do, then ? :)

What I'm arguing about is a matter of name resolution. I sure
can open a scope 'A' in C++, insert a new scope 'B' into it,
and from within that access other symbols declared in scope 'A'.
In fact, I don't need to qualify them (i.e. 'foo' instead of 'A::foo'
for any symbol 'foo' in the scope 'A' is just fine) if I'm inside
that scope, no matter how deeply nested (of course, as long as there
are no ambiguities).



> 
> >>> class A:
> ...     prefix = 'usr/local'
> ...
> >>> class B(A):
> ...     datadir = A.prefix+'/share'
> ...
> >>> A.prefix
> 'usr/local'
> >>> B.datadir
> 'usr/local/share'

ok, that works because B derives from A, i.e. the definition of 'A'
is complete at the point where B's definition starts.

> >>> class A:
> ...     prefix = os.path.join('usr','local')
> ...
> >>> class B(A):
> ...     datadir = os.path.join(A.prefix, 'share')

good idea, thanks. the original 'prefix' stems from 'configure', i.e.
my python file 'foo.py' is generated from 'foo.py.in' by means of the
autotools (autoconf, configure, ...)

Thanks,
		Stefan



More information about the Python-list mailing list