-1

I'm taking a Programming class, and for one of the tests I need to make a class that inherits from an already created class. This is the code the teacher taught me to do, but it doesn't seem to be working:

class Intern(Employer):
    def __init__(self, last_name, first_name, address, phone, email, end_date):
        Employer(last_name, first_name, address, phone, email)
        self.end_date=end_date
    def intern_info(self):
        self.print_info()
        print self.end_date

3 Answers3

6

The line

Employer(last_name, first_name, address, phone, email)

creates an Employer object that is immediately garbage-collected as the execution of __init__() ends. It does not affect the Intern object in any way. What you mean is probably something like

Employer.__init__(self, last_name, first_name, address, phone, email)

or maybe

super(Intern, self).__init__(last_name, first_name, address, phone, email)
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • I tried creating an object from that and it said: File "", line 3, in __init__ super(Employer, self).__init__(last_name, first_name, address, phone, email) TypeError: must be type, not classobj – Josh Livingston Dec 17 '12 at 17:38
  • @JoshLivingston You can probably work around this if you define the `Employer` class as `class Employer(object): ...`. This way `Employer` will be a [*new style class*](http://stackoverflow.com/q/54867/1258041), which is what you should use when writing new code. – Lev Levitsky Dec 17 '12 at 17:40
  • Okay, I did the Employer.__init__(self, last_name...) and it worked. Now, I need help with the function below it, def intern_info(self):... it should take the print_info() function from the Employer class, which prints last_name, first_name, and all of the other attributes from Employer. It gives me this error when I call the intern_info() function: AttributeError: Intern instance has no attribute 'print_info' – Josh Livingston Dec 17 '12 at 17:48
  • @JoshLivingston The code you show in the question should work assuming that you defined `print_info()` in the `Employer` class. – Lev Levitsky Dec 17 '12 at 17:50
  • 2
    I hope you meant `super(Intern, self).__init__(last_name, first_name, address, phone, email)` for the last one. You need to pass the current class as the first parameter and super will find the next base from it, if you pass `Employer` to super you'll bypass `Employer`'s `__init__`. – Duncan Dec 17 '12 at 18:03
  • @Duncan Thank you, I tend to mix that up since the first time I read the docs and was confused as to why I need to pass the object's class if I pass the object anyway :) – Lev Levitsky Dec 17 '12 at 18:17
2

You are missing self in the arguments to Employer. You either need to use super or pass self explicitly to the parent's __init__. You also need to explicitly call the parent's __init__ method.

class Intern(Employer):
    def __init__(self, last_name, first_name, address, phone, email, end_date):
        Employer.__init__(self, last_name, first_name, address, phone, email)
        self.end_date=end_date
    def intern_info(self):
        self.print_info()
        print self.end_date
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
Silas Ray
  • 25,682
  • 5
  • 48
  • 63
0

You are missing the indentation which is the mechanism for code block in Python.

Try:

class Intern(Employer):
    def __init__(self, last_name, first_name, address, phone, email, end_date):
        Employer(last_name, first_name, address, phone, email)
        self.end_date=end_date
    def intern_info(self):
        self.print_info()
        print self.end_date
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117