12

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.)

gsamaras
  • 71,951
  • 46
  • 188
  • 305
sid597
  • 999
  • 3
  • 16
  • 31

9 Answers9

7

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)
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
4

It was given that:

class ArrogantProfessor(Professor): 

but you did this:

class ArrogantProfessor(Person): 

which resulted in the halved grade.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Actually I first used Professor as the argument but that did not work so I changed that to Person – sid597 Mar 12 '16 at 22:56
  • 1
    That was the goal of the assigment @johnsmith! To make you *think* how to make it work with `Professor`. Good question btw, you got my upvote. – gsamaras Mar 12 '16 at 22:57
2

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.

user2390182
  • 72,016
  • 6
  • 67
  • 89
2

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.

Goodies
  • 4,439
  • 3
  • 31
  • 57
2

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.

Vasili Korol
  • 59
  • 3
  • 9
2

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.

willnx
  • 1,253
  • 1
  • 8
  • 14
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)
0

For the second part, the correct answer is:

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

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

Jean
  • 1