nested classes

Stefan Seefeld seefeld at sympatico.ca
Tue Jun 19 16:14:29 EDT 2001


gbreed at cix.compulink.co.uk wrote:

> You're correct, I've found an example
> 
> <http://oopsla.snu.ac.kr/c++/cpplecture/chap5/node22.html>
> 
> and an argument against them
> 
> <http://www.elj.com/eiffel/ij/nested-classes/>

ok, well, this is a python list so let's not turn this into a C++
discussion. However, the above link contains a couple of wrong
statements. To get this back on track, only this:

class nesting uses the fact that a class (in C++) acts as an ordinary
scope, so you can use it the same way as a namespace, to bind names
to a common scope, and avoid name clashes if the name you want to define
is general, but has a specific meaning in the scope within which you want
to use it. ('Geometry::Point' for example).

Back to my python code:

I tried to mirror the structure of a package I provide ('Synopsis') in
a Config class. Since the synopsis tool is very modular, each submodule
contains its own set of parameters which you might want to configure.
Passing all of them as command line options will quickly get very messy,
so we figured we might just provide the possibility to write a config
script that sets all the flags/options at the right scope. For example:
Some options apply to more than one module, so it appears natural to
define it in an 'outer scope'. This, however, assumes that 'inner scopes'
(aka nested classes) can see the variables defined in outer scopes.

I hope this somewhat explains what I'm aiming at...


> But my head still spins when I try to think of a C++ class that
> hasn't been instantiated.

Again, we are talking about classes as scopes, not class instances.

> class A:
>   def __init__(self):
>     self.prefix = 'usr/local'
>   class __B:
>     def __init__(self, parent):
>       self.prefix = os.path.join(parent.prefix, 'share')
>   def B(self):
>     return self.__B(self)
> 
> print A().prefix
> print A().B().prefix
> 
> You could remember the parent, but that'd lead to a circular
> reference (not such a bad thing for 2.x).

right. This gets pretty messy too, as you are forced to use instances,
while I was trying to avoid that. In fact, I could have used modules
instead of classes, but since python constrains me to one module per file,
I tried to get away with something a bit finer grained.

> Perhaps if you give us C++ code of what you're trying to do, we
> can tell you why you don't need to do it ;)

right. That's exactly what I want. Please show me the right idiom to use
in python to solve the above problem.

And by the way, the project I'm talking about is 'synopsis', at
http://synopsis.sourceforge.net/, a modular source code inspection tool
with parsers for IDL, C++, python, and a couple of output formatters.
The module structure is this:

Synopsis/
	Core/
		Util
		AST
		Type
	Parser/
		IDL
		C++
		Python
	Linker/
		Linker
	Formatter/
		HTML
		TexInfo
		DocBook
		Dot
		ASCII
		...

and each node in this tree has its own set of parameters.
Online docs are at http://synopsis.sourceforge.net/docs/index.html

Regards,
		Stefan



More information about the Python-list mailing list