Skip to content Skip to sidebar Skip to footer

How To Do Nested Class And Inherit Inside The Class

I tried quite a few times, but didn't get the following codes work Thanks in advance for any help/suggestions class A(object): def __init__(self,x,y,z): self.c=C(x,y,z

Solution 1:

I'm not 100% sure why you're nesting classes (rarely is that what you actually want to do) but the line

self.c = C(x,y,z)

is almost certainly the problem here. Unless I don't understand what it is you're trying to accomplish (which may well be), you should be able to do

classA(object):

    def__init__(self, x, y, z):
        self.c = self.C(x,y,z)

    defgetxyz(self):
        return self.c.getxyz()

    classB(object):

        def__init__(self, x, y):
            self.x = x
            self.y = y

        defgetxy(self):
            return self.x, self.y

    classC(B):

        def__init__(self, x, y, z):
            super(A.C, self).__init__(x,y)
            self.z = z

        defgetxyz(self):
            (x,y) = self.getxy()
            return x, y, self.z

Post a Comment for "How To Do Nested Class And Inherit Inside The Class"