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


Groups > comp.lang.python > #3855

Re: A question about Python Classes

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=086d8ad5d=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.04; 'instance': 0.05; 'constructor': 0.07; 'pascal': 0.07; 'refers': 0.07; 'python': 0.07; 'context.': 0.09; 'stating': 0.09; '>>>': 0.12; 'def': 0.13; 'wrote:': 0.14; 'chad': 0.16; "didn't,": 0.16; 'instance:': 0.16; 'mrab': 0.16; 'pov,': 0.16; 'received:192.168.200': 0.16; 'subject:Classes': 0.16; 'using,': 0.16; 'class,': 0.16; 'writes:': 0.20; 'language': 0.20; '(like': 0.22; 'header:In-Reply-To:1': 0.22; 'subject:question': 0.22; 'example': 0.24; 'object': 0.27; 'pass': 0.27; 'class': 0.29; 'about.': 0.29; 'class.': 0.29; 'oop': 0.31; 'trick': 0.31; 'anyone': 0.31; 'to:addr:python-list': 0.32; 'created': 0.33; 'test': 0.33; 'sometimes': 0.33; 'using': 0.34; 'received:192': 0.34; 'there': 0.35; 'print': 0.35; 'question': 0.35; 'header :User-Agent:1': 0.35; 'formal': 0.35; 'trigger': 0.35; 'think': 0.36; "we're": 0.37; 'received:192.168': 0.37; 'some': 0.37; 'however': 0.37; 'but': 0.38; 'to:addr:python.org': 0.39; 'how': 0.39; 'skip:h 20': 0.60; 'subject:about': 0.66; 'concept': 0.73; 'one!': 0.84; 'inheritance,': 0.91
X-IronPort-AV E=Sophos;i="4.64,253,1301868000"; d="scan'208";a="1289669"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Fri, 22 Apr 2011 11:47:18 +0200
From Jean-Michel Pichavant <jeanmichel@sequans.com>
User-Agent Mozilla-Thunderbird 2.0.0.24 (X11/20100328)
MIME-Version 1.0
To python-list@python.org
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>
In-Reply-To <4DB070A8.8090704@mrabarnett.plus.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.739.1303465654.9059.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 82.94.164.166
X-Trace 1303465654 news.xs4all.nl 81484 [::ffff:82.94.164.166]:41648
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3855

Show key headers only | 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