Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3890

Re: A question about Python Classes

Newsgroups comp.lang.python
Date 2011-04-22 12:47 -0700
Subject Re: A question about Python Classes
From Carl Banks <pavlovevidence@gmail.com>
Message-ID <mailman.763.1303501625.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Thursday, April 21, 2011 11:00:08 AM UTC-7, MRAB wrote:
> On 21/04/2011 18:12, Pascal J. Bourguignon wrote:
> > chad<cda...@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!
> >
> No, he didn't, he created an instance of HomeHandler.
> 
> > test is an instance of HomeHandler, which is a subclass of BaseHandler,
> > so test is also an instance of BaseHandler.
> >
> test isn't really an instance of BaseHandler, it's an instance of
> HomeHandler, which is a subclass of BaseHandler.

I'm going to vote that this is incorrect usage.  An instance of HomeHandler is also an instance of BaseHandler, and it is incorrect to say it is not.  The call to HomeHandler does create an instance of BaseHandler.

The Python language itself validates this usage.  isinstance(test,BaseHandler) returns True.


If you are looking for a term to indicate an object for which type(test) == BaseHandler, then I would suggest "proper instance".  test is an instance of BaseHandler, but it is not a proper instance.


Carl Banks

Back to comp.lang.python | Previous | NextNext in thread | Find similar


Thread

Re: A question about Python Classes Carl Banks <pavlovevidence@gmail.com> - 2011-04-22 12:47 -0700
  Re: A question about Python Classes chad <cdalten@gmail.com> - 2011-04-23 13:30 -0700
    Re: A question about Python Classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-24 00:03 +0000

csiph-web