5

Okay I'm new to OOP, and my problem is that I have this parent class and it has a method info in it.I want to reuse the two print statements within that method for its subclass inside a method of the same name and add more information to it. but I can't get it to work

class company:

    def __init__(self, fName, lName, salary):

        self.fName = fName
        self.lName = lName
        self.salary = salary

    def info(self):
        print("Name:", self.fName, self.lName)
        print("Salary:", self.salary)

class programmers(company):
    def __init__(self, fName, lName, salary, progLang):

        super().__init__(fName, lName, salary)
        self.progLang = progLang

    def info(self):
        print("Programming language:", self.progLang)

programmer1 = programmers("John", "Doe", 1000, "Python")
programmer1.info()

I thought of just rewriting the lines of code I want to reuse but I though that would take the point out of OOP.

I'm looking for this kind of output...

Name: John Doe
Salary: 1000
Programming language: Python

I'm using Python 3

Thanks in advance!

Acubal
  • 173
  • 1
  • 2
  • 8

1 Answers1

1

Try out below code. Your parent class should inherit from "object". super only works for new style classes

class company(object):

    def __init__(self, fName, lName, salary):

        self.fName = fName
        self.lName = lName
        self.salary = salary

    def info(self):
        print("Name:", self.fName, self.lName)
        print("Salary:", self.salary)

class programmers(company):
    def __init__(self, fName, lName, salary, progLang):

        super(programmers, self).__init__(fName, lName, salary)
        self.progLang = progLang

    def info(self):
        super(programmers, self).info()
        print("Programming language:", self.progLang)

programmer1 = programmers("John", "Doe", 1000, "Python")
programmer1.info()
Akshay
  • 1,231
  • 1
  • 19
  • 31
  • I did as you said but i received an attributeError – Acubal Oct 30 '16 at 03:29
  • 4
    In Python 3, you don't need to pass those arguments to `super`, nor do you need to inherit from `object`, that happens by default (only "new style" classes) – juanpa.arrivillaga Oct 30 '16 at 03:49
  • @acubal However, this still *should* work. What error are you getting exactly? – juanpa.arrivillaga Oct 30 '16 at 03:51
  • @jaunpa.arrivillaga : That is correct. Thanks. – Akshay Oct 30 '16 at 03:53
  • @acubal: Actual problem with your code was missing inheritance from object. – Akshay Oct 30 '16 at 03:53
  • @Akshay No, not it is Python 3. All classes are new-style, there are no old-style classes in Python 3. – juanpa.arrivillaga Oct 30 '16 at 03:56
  • @jaunpa.arrivillaga: You made right point but generally porting between Python 2.7 and 3 becomes hard because of legacy code and I still prefer to go by things which are supported in both. :) Just posting a link out of interest to understand further. https://docs.python.org/3/howto/pyporting.html#subclass-object but of course if given a choice, going with new things will be always better. – Akshay Oct 30 '16 at 04:08
  • @Akshay I actually agree with what you say, and I always explicitly subclass from `object`, but it isn't the cause of whatever error OP is encountering. That is simply my point. – juanpa.arrivillaga Oct 30 '16 at 04:18
  • @Akshay, thank you very much it works now : ) – Acubal Oct 30 '16 at 23:03