3

Is it possible to have inheritance for inner classes like it is with subclass(object)? How would I do it with the below code for instance?

class main:
    class sub(main):
        def __init__(self):
            print(main.foo)
    def __init__(self):
        self.foo = "bar"
        baz = self.sub()

if __name__ == '__main__':
    bar = main()
5
  • Why do you need them to be "inner classes"? It would inherit itself, which would seem strange... Commented Nov 14, 2013 at 2:16
  • How would that even work? By that definition sub would also have an inner class called sub and so on. Commented Nov 14, 2013 at 2:19
  • @rogaos They're probably inner classes to indicate that they're meant to be "private" to that class. Commented Nov 14, 2013 at 2:27
  • 1
    I think it's impossible, because when you define 'sub', the class 'A' has not been created yet. Commented Nov 14, 2013 at 2:46
  • Let me rephrase, I want the nested/inner/whateveryouwanttocallthem classes to be able to directly edit their parent/nest/WYWTCT's attributes like you can do with the example I linked to. Commented Nov 14, 2013 at 4:27

1 Answer 1

1

It is kind of confusing what you are looking for, in particular do you want instances of main and sub to be 'nested' or do you actually want nested classes, which is a very unusual thing to want. You can't have a nested class that subclasses its enclosing

You can replicate what I think you are trying to do with simple composition:

class Main(object):
    def __init__(self):
        self.foo = 'bar'
        baz = Helper(self)

class Helper(object):
    def __init__(self, parent):
        self.parent = parent  
        print(parent.foo) # Access to members of Main through parent reference

if __name__ == "__main__":
    x = Main()

I have called the auxiliary class helper because it is not a subclass, it is just a helper class. This is a bit dubious. If these two classes are so tightly coupled there is always a 1:1 relationship between them and they both need to hold references to the other, it isn't clear why they are separate classes. You can also do something similar with regular inheritance, and I think this might be more like what you want.

class Main(object):
    def __init__(self):
        self.foo = 'bar'
class Sub(main):
    def __init__(self):
        super(Sub, self).__init__()
        print(self.foo)  # All attributes defined by main exist in sub
if __name__ == "__main__":
    Sub()

Notice that we construct the sub object which contains all the behavior from the main class as well as its own. Anyway, I hope this helps. If you can update your question to explain how the behavior you want differs from these, we can go from there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.