1
class Config_A(test.Case):
    def insert(self, protocol_id):
        ...
        ...


class Config_B(test.Case):
    ...
    ....
    ....

    def test_somecase(self):
        self.insert(10)

Here def insert() is a common method, which is required by multiple classes. From class Config_B, its not able to access the definition of insert() defined in Config_A. One solution is that, define it in Config_B and make use of. But it's a duplication of code. How can we do this without writing duplicate code?

3
  • How familiar are you with inheritance? Commented Sep 17, 2018 at 6:09
  • You can use inheritance, and you'll be able to access methods of parent class. In your case it'd be, class config_B(Config_A, test.Case):. Commented Sep 17, 2018 at 6:09
  • see this if it helps! stackoverflow.com/questions/2797139/… Commented Sep 17, 2018 at 6:13

1 Answer 1

1
class Config_A():
    def insert(self, protocol_id):
        print 'protocol_id=',protocol_id

class Config_B(Config_A):
    def test_somecase(self):
        Config_A().insert(10)

def main():
    obj = Config_B()
    obj.test_somecase()

if __name__ == '__main__':
    main()

This one worked.

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

1 Comment

Config_A().insert(10) can be replaced by self.insert(10). Otherwise, you create a temporary instance that is immediately destroyed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.