nested class problem

Stephane Ninin Stephane.Ninin at fr.invalid
Sat Jan 24 21:27:49 CET 2004

  Hello all,

I am trying to play with nested class in a script I am making,
and I am not sure I really understand how they work.

Here is some code:

__all__ = ["ITest"]

from exceptions import Exception

class ITest(object):

    def __init__(self):
        if (self.__class__ == ITest):
            raise Exception("ITest cannot be instanciated")

    def read(self,filename):
        self.__filename = filename
    def _getFilename(self):
        return self.__filename
    def make_reader():
        return _PTest()
    make_reader = staticmethod(make_reader)

class _PTest(ITest):

    class _PHandler(object):
        def __init__(self):

        def test(self):
    def __init__(self):

    def read(self,filename):
        print "HERE"
        print dir()
        print dir(self)
        print self.__class__
        print dir(self.__class__)
        dh = self._PHandler()

if __name__=='__main__':

    print dir(a)"")
    print b

I want to put class _PHandler in _Ptest,
and use _PHandler in _Ptest methods,
but anything I try doesnot work...

On this configuration, I have:
Traceback (most recent call last):
  File "./", line 58, in ?"")
  File "./", line 51, in read
    dh = self._PHandler()
  File "./", line 34, in __init__
NameError: global name '_PHandler' is not defined

and I have similar problems if I try to access _PHandler
in different ways...
Is this possible somehow ? Or what is the problem with my approach ?
I know I could just dont use nested classes.., but I'd like to try. 

   Thanks in advance. 


Stephane Ninin

More information about the Python-list mailing list