[Edu-sig] Python @ Education: What are your problems?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 7 Jun 2002 12:21:14 -0700 (PDT)


On Fri, 7 Jun 2002, Guido van Rossum wrote:

> > ###
> > >>> class Parent:
> > ...     def sayHello(self):
> > ...         print "hello"
> > ...
> > >>> class Child(Parent):
> > ...     def sayHello(self):
> > ...         print "My mom says:",
> > self.__class__.__bases__[0].sayHello(self)
> > ...
> > >>> p, c = Parent(), Child()
> > >>> p.sayHello()
> > hello
> > >>> c.sayHello()
> > My mom says: hello
> > ###
> >
> > but as you can tell, this is really awkward!
>
> It is also wrong.  Consider
>
> >>> class GrandChild(Child): pass
> ...
> >>> g = GrandChild()
> >>> g.sayHello()
> (infinite loop)


Doh.  Let me try that one more time.

###
class Parent:
    def sayHello(self):
        print "Hello"

class Child(Parent):
    def sayHello(self):
        print "My mom says:", Parent.sayHello(self)
###

Thanks for the correction!