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


Groups > comp.lang.python > #3855

Re: A question about Python Classes

Date 2011-04-22 11:47 +0200
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: A question about Python Classes
References <2219ee53-e8aa-4ac4-839f-014c3d1b1914@a19g2000prj.googlegroups.com> <87k4enil5g.fsf@kuiper.lan.informatimago.com> <4DB070A8.8090704@mrabarnett.plus.com>
Newsgroups comp.lang.python
Message-ID <mailman.739.1303465654.9059.python-list@python.org> (permalink)

Show all headers | View raw


MRAB wrote:
> On 21/04/2011 18:12, Pascal J. Bourguignon wrote:
>> chad<cdalten@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.
I think this is really wrong within the OP question context. This is a 
key concept about OOP and inheritance, any object of class HomeHandler 
is an object of class BaseHandler and also an object of any other base 
class.

However it is true that for convenience, there are some language abuses 
around this terms that we're all using, for instance:
  - class refers to the lowest class of the object (like the python 
__class__ attribute)
  - "instance of" means sometimes "created using the constructor of" 
when stating for example  that you cannot create an instance of a 
virtual class. From a formal OO POV, anyone can create an instance of a 
virtual class, you just need to create an instance of one of its 
subclass. What you cannot, is create a instance of a virtual class using 
the constructor of that virtual class.

Also note that
isinstance(test, BaseHandler)
returns True. And this is no Python trick to trigger some magic, this is 
what OO is about.


JM

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


Thread

A question about Python Classes chad <cdalten@gmail.com> - 2011-04-21 08:43 -0700
  Re: A question about Python Classes Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-04-21 18:30 +0200
    Re: A question about Python Classes chad <cdalten@gmail.com> - 2011-04-21 09:46 -0700
  Re: A question about Python Classes "Pascal J. Bourguignon" <pjb@informatimago.com> - 2011-04-21 19:12 +0200
    Re: A question about Python Classes MRAB <python@mrabarnett.plus.com> - 2011-04-21 19:00 +0100
      Re: A question about Python Classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-22 01:53 +0000
    Re: A question about Python Classes Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-04-22 11:47 +0200
  Re: A question about Python Classes Terry Reedy <tjreedy@udel.edu> - 2011-04-21 13:39 -0400
  Re: A question about Python Classes Ethan Furman <ethan@stoneleaf.us> - 2011-04-21 11:34 -0700
    Re: A question about Python Classes "Kyle T. Jones" <onexpadREMOVE@EVOMERyahoodotyouknow.com> - 2011-04-22 08:49 -0500
      Re: A question about Python Classes Ethan Furman <ethan@stoneleaf.us> - 2011-04-22 12:38 -0700
      Re: A question about Python Classes Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-22 13:40 -0600

csiph-web