0

I am trying call a child class method using parent class constructor.

class Configurator():
    config_file = "" #variable which stores config file
    input_response = "" #variable which stores input response json

    def __init__(self,config_file, input_response):
        self.config_file = config_file
        self.input_response = input_response

        config = ConfigParser.ConfigParser()
        config.read('config.cfg')
        if config.get('Configurator', 'ctool')  ==  'Chef':
            Configurator.__initialize(self)

    def __initialize(self):
        open_input_response = open(self.input_response, 'r')
        read_cloudprovider = json.load(open_input_response)
        cloudprovider = read_cloudprovider['CloudProvider']
        if cloudprovider == 'AWS':
            print('Working Here')
            obj = AWS()
            obj.print_test(self)


class AWS(Configurator):
    def print_test(self):
        print('I am AWS Class')


def main():
    configurator = Configurator('config.cfg', 'input_response.json')


if __name__ == '__main__':
    main()

Error:

TypeError: __init__() takes exactly 3 arguments (1 given)

Why im getting an error, AWS has no orguments to take

4
  • 1
    Side note: I strongly encourage you to check PEP 8 (and PEP 257): this will make your code more legible for you and others. This will in particular make it easier to answer your questions on StackOverflow. Commented Aug 7, 2014 at 13:19
  • The answers below are quite good. But it is quite odd that you are having your base class instantiate an instance of something that is a child class of itself. It implies that there is something odd going on in your code. Commented Aug 7, 2014 at 13:19
  • 1
    @EOL thanks I will make sure that I will follow PEP 8 Commented Aug 7, 2014 at 13:42
  • "Use the white-spaces, Luke!" Commented Aug 7, 2014 at 13:56

2 Answers 2

2

Methods that are not overridden in the child are the same method as in the parent. AWS.__init__() takes two additional arguments.

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

Comments

1

Take a look at the following segment:

 obj=AWS()
 obj.print_test(self)

You are constructing a new instance of the class AWS. What should python do when creating that instance? It must call AWS.__init__ but that method is nonexistent so it uses its parent - Configuration.__init__. The latter takes 3 arguments and this is why you see that error.

To fix that, you need to explicitly call Configuration.__init__ with the correct arguments:

class AWS:
    ...
    def __init__(self):
        Configuration.__init__(self, "value_for_config", "value_for_input")
        ...

Also, please note that in your current code Configuration.__init__ calls AWS.__init__ which calls Configuration.__init__ once again, this indirect recursion can easily lead to stack overflow so be careful.

1 Comment

Error occurred while calling Configurator._init__ from AWS.__init__ Error: IOError: [Errno 2] No such file or directory: ''

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.