sid597
sid597

Reputation: 1019

Class inheritance in python

I am solving this problem :

Consider the following hierarchy of classes:

class Person(object):     
    def __init__(self, name):         
        self.name = name     
    def say(self, stuff):         
        return self.name + ' says: ' + stuff     
    def __str__(self):         
        return self.name  

class Lecturer(Person):     
    def lecture(self, stuff):         
        return 'I believe that ' + Person.say(self, stuff)  

class Professor(Lecturer): 
    def say(self, stuff): 
        return self.name + ' says: ' + self.lecture(stuff)

class ArrogantProfessor(Professor): 
    def say(self, stuff): 
        return 'It is obvious that ' + self.say(stuff)

As written, this code leads to an infinite loop when using the Arrogant Professor class.

Change the definition of ArrogantProfessor so that the following behavior is achieved:

e = Person('eric') 
le = Lecturer('eric') 
pe = Professor('eric') 
ae = ArrogantProfessor('eric')

e.say('the sky is blue')              #returns   eric says: the sky is blue

le.say('the sky is blue')             #returns   eric says: the sky is blue

le.lecture('the sky is blue')         #returns   believe that eric says: the sky is blue

pe.say('the sky is blue')             #returns   eric says: I believe that eric says: the sky is blue

pe.lecture('the sky is blue')     #returns   believe that eric says: the sky is blue

ae.say('the sky is blue')         #returns   eric says: It is obvious that eric says: the sky is blue

ae.lecture('the sky is blue')     #returns   It is obvious that eric says: the sky is blue

My solution to this is:

class ArrogantProfessor(Person):
    def say(self, stuff):
        return Person.say(self, ' It is obvious that ') +  Person.say(self,stuff)
    def lecture(self, stuff):
        return 'It is obvious that  ' + Person.say(self, stuff)

But the checker gives only half marks for this solution. What is the mistake that I am making and what are the test cases on which this code fails? (I am new to python and learned about classes some time ago.)

Upvotes: 12

Views: 4051

Answers (9)

Jean
Jean

Reputation: 1

for part one the code is:

class ArrogantProfessor( Professor ):
def lecture(self, stuff):
    return 'It is obvious that ' +  Person.say(self,stuff)

for part two the code is:

class ArrogantProfessor(Professor):
def lecture(self, stuff):
    return 'It is obvious that I believe that ' +  Person.say(self,stuff)

for part three the code is:

class Professor(Lecturer):
 def say(self, stuff): 
     return 'Prof. ' + self.name + ' says: ' + self.lecture(stuff)

Hope it is usefull

Upvotes: 0

IgorMatkovic
IgorMatkovic

Reputation: 1

For the second part, the correct answer is:

class ArrogantProfessor(Professor):
    def lecture(self, stuff):
        return 'It is obvious that ' +  Lecturer.lecture(self,stuff)

Upvotes: 0

     class Professor(Lecturer): 
        def say(self, stuff): 
            return "Prof. " + self.name + ' says: ' + self.lecture(stuff)

     class ArrogantProfessor( Professor ):
        def lecture(self, stuff):         
            return 'It is obvious that I believe that ' + Person.say(self, stuff)

Upvotes: 0

Mike Müller
Mike Müller

Reputation: 85442

You probably should use super() instead of hard-wiring the class Person:

class ArrogantProfessor(Person):
    def say(self, stuff):
        return super(ArrogantProfessor, self).say(self.lecture(stuff))
    def lecture(self, stuff):
        return 'It is obvious that ' + super(ArrogantProfessor, self).say(stuff)

Upvotes: 7

Vasili Korol
Vasili Korol

Reputation: 81

This should be:

class ArrogantProfessor( Professor ):
    def lecture(self, stuff):
        return 'It is obvious that ' +  Person.say(self,stuff)

You don't have to define say() in ArrogantProfessor, because it is already defined in Professor, and it will use the lecture() method defined in the child class.

Upvotes: 2

willnx
willnx

Reputation: 1283

It's a bit hard to say without knowing what they're trying to teach you. A likely guess is that you're being taught inheritance, and if they've gone over super it's likely that they want you to utilize it to have the ArrogantProfessor's output look like:

eric says: It is obvious that STUFF

Where STUFF is the string you're passing in.

Upvotes: 2

gsamaras
gsamaras

Reputation: 73366

It was given that:

class ArrogantProfessor(Professor): 

but you did this:

class ArrogantProfessor(Person): 

which resulted in the halved grade.

Upvotes: 4

Goodies
Goodies

Reputation: 4681

He probably wants you to actually get the parent class. The way to do this is simple.

Python2/3:

class ArrogantProfessor(Professor):
    def say(self, stuff):
        return 'It is obvious that ' + super(ArrogantProfessor, self).say(stuff)

Python 3 only:

class ArrogantProfessor(Professor):
    def say(self, stuff):
        return 'It is obvious that ' + super().say(stuff)

In either case, ae.say("something") should return:

"It is obvious that eric says: I believe that eric says: something"

This is because the parent class is Professor, not Person.

Similarly, in your lecture class, you should do:

def lecture(self, stuff):
    return 'I believe that ' + super(Lecturer, self).say(self, stuff) # or the Python3 version if you're using that

It's not really clear what it is that you want, though.

Upvotes: 2

user2390182
user2390182

Reputation: 73460

As a former grader of coding hw, I assume, you should have produced the desired output without making ArrogantProfessor a mere Person. After all, the class name indicates that it should still subclass Professor.

Upvotes: 2

Related Questions