nested class problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephane Ninin

    nested class problem

    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("ITes t cannot be instanciated")

    def read(self,filen ame):
    self.__filename = filename

    def _getFilename(se lf):
    return self.__filename

    def make_reader():
    return _PTest()
    make_reader = staticmethod(ma ke_reader)



    class _PTest(ITest):

    class _PHandler(objec t):

    def __init__(self):
    super(_PHandler ,self).__init__ ()
    #self.__map={}

    def test(self):
    pass

    def __init__(self):
    super(_PTest,se lf).__init__()


    def read(self,filen ame):
    super(_PTest,se lf).read(filena me)
    print "HERE"
    print dir()
    print dir(self)
    print self.__class__
    print dir(self.__clas s__)
    dh = self._PHandler( )
    #dh.test()

    if __name__=='__ma in__':

    a=ITest.make_re ader()
    print dir(a)
    b=a.read("")
    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 "./test.py", line 58, in ?
    b=a.read("")
    File "./test.py", line 51, in read
    dh = self._PHandler( )
    File "./test.py", line 34, in __init__
    super(_PHandler ,self).__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.

    Regards,






    --
    Stephane Ninin

  • Peter Otten

    #2
    Re: nested class problem

    Stephane Ninin wrote:
    [color=blue]
    > 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.[/color]

    [..][color=blue]
    >
    > class _PTest(ITest):
    >
    > class _PHandler(objec t):
    >
    > def __init__(self):
    > super(_PHandler ,self).__init__ ()[/color]

    Make that
    super(_PTest._P Handler, self).__init__( )

    or, even better, omit it entirely. Python class hierarchies tend to remain
    flat, because inheritance is only necessary for code reuse, not to indicate
    a particular interface.
    [color=blue]
    > #self.__map={}
    >
    > def test(self):
    > pass
    >
    > def __init__(self):
    > super(_PTest,se lf).__init__()
    >
    >
    > def read(self,filen ame):
    > super(_PTest,se lf).read(filena me)
    > print "HERE"
    > print dir()
    > print dir(self)
    > print self.__class__
    > print dir(self.__clas s__)
    > dh = self._PHandler( )
    > #dh.test()
    >
    > if __name__=='__ma in__':
    >
    > a=ITest.make_re ader()
    > print dir(a)
    > b=a.read("")
    > print b
    >
    >
    >
    >
    > I want to put class _PHandler in _Ptest,
    > and use _PHandler in _Ptest methods,
    > but anything I try doesnot work...[/color]

    I think that's you fighting against the language and - a rare case - python
    fighting back :-)
    [color=blue]
    >
    > On this configuration, I have:
    > Traceback (most recent call last):
    > File "./test.py", line 58, in ?
    > b=a.read("")
    > File "./test.py", line 51, in read
    > dh = self._PHandler( )
    > File "./test.py", line 34, in __init__
    > super(_PHandler ,self).__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.[/color]

    I fail to see the benefit of nested classes. If you want to bring some
    structure into your python application, try packages instead.

    Peter

    Comment

    Working...