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!

5
  • 5
    super is your friend: blog.pythonlibrary.org/2014/01/21/python-201-what-is-super Commented Oct 30, 2016 at 2:24
  • You should describe what you tried that "didn't work" Commented Oct 30, 2016 at 2:25
  • I want to take the two print statements in the parent class info method and add more info to it on the programmers class info method Commented Oct 30, 2016 at 2:27
  • Mmm... In your model, a 'programer' IS_A 'company' How is it possible? Commented Oct 30, 2016 at 2:57
  • Then you must use the appropriate relation. For example, your 'company' could have a list of programmers. And each programmer, have another list, for the companies where he works. Commented Oct 30, 2016 at 3:10

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

9 Comments

I did as you said but i received an attributeError
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)
@acubal However, this still should work. What error are you getting exactly?
@jaunpa.arrivillaga : That is correct. Thanks.
@acubal: Actual problem with your code was missing inheritance from object.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.