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

2
  • Is that a typo in the solution for le.lecture(‘the sky is blue’), or is there really the pronoun "I" missing? Commented Mar 12, 2016 at 22:31
  • @L3viathan that was a typo Commented Mar 12, 2016 at 22:33

9 Answers 9

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

Comments

4

It was given that:

class ArrogantProfessor(Professor): 

but you did this:

class ArrogantProfessor(Person): 

which resulted in the halved grade.

2 Comments

Actually I first used Professor as the argument but that did not work so I changed that to Person
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.
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.

Comments

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.

Comments

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.

Comments

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.

Comments

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)

1 Comment

You should have context for your answer, not just code.
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)

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.