0

I’m looking for a nice solution to avoid code duplication, my code look like this;

class HostEnvironment(AbstractEnvironment):

    def provision(self, wait_for_sshd=True):

        some code

    def __init__(self, layer_info):

        pass

class VCBEnvironment(HostEnvironment):

    def provision(self, wait_for_sshd=True):

        same code

        plus some more code

    def __init__(self, layer_info):
        super(VCBEnvironment, self).__init__(layer_info)
2
  • 1
    You are already using super() in the __init__ method. Why can't you use that instead of same code in VCBEnvironment.provision() exactly? Commented Jun 30, 2015 at 7:44
  • @MartijnPieters: He might not know that super can be used for methods besides the __init__ method. Commented Jun 30, 2015 at 18:21

1 Answer 1

1

E.g. like this:

class AbstractEnvironment:
    pass

class HostEnvironment (AbstractEnvironment):
    def __init__ (self, layer_info):
        pass

    def provision (self, wait_for_sshd = True):
        print 'some code'

class VCBEnvironment (HostEnvironment):
    def __init__(self, layer_info):
        HostEnvironment.__init__ (self, layer_info)

    def provision (self, wait_for_sshd = True):
        HostEnvironment.provision (self, wait_for_sshd)
        print 'some more code'

print '\ne1'
e1 = HostEnvironment (None)
e1.provision ()

print '\ne2'
e2 = VCBEnvironment (None)
e2.provision ()

Output:

e1
some code

e2
some code
some more code
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.