A question about Python Classes

Pascal J. Bourguignon pjb at informatimago.com
Thu Apr 21 19:12:11 CEST 2011

chad <cdalten at gmail.com> writes:

> Let's say I have the following....
> class BaseHandler:
>     def foo(self):
>         print "Hello"
> class HomeHandler(BaseHandler):
>     pass
> Then I do the following...
> test = HomeHandler()
> test.foo()
> How can HomeHandler call foo() when I never created an instance of
> BaseHandler?

But you created one!

test is an instance of HomeHandler, which is a subclass of BaseHandler,
so test is also an instance of BaseHandler.

A subclass represents a subset of the instances of its super class.

__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.

More information about the Python-list mailing list