简体   繁体   中英

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

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)

It was given that:

class ArrogantProfessor(Professor): 

but you did this:

class ArrogantProfessor(Person): 

which resulted in the halved grade.

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 .

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.

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.

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.

     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)

For the second part, the correct answer is:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM