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


Groups > comp.lang.python > #3814

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 <python-python-list@m.gmane.org>
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; 'builtins': 0.07; 'cpython': 0.07; 'default,': 0.07; 'terry': 0.07; 'attribute': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'specific.': 0.09; '>>>': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'chad': 0.16; 'class;': 0.16; 'inherited': 0.16; 'instance;': 0.16; 'lookup': 0.16; 'reedy': 0.16; 'subject:Classes': 0.16; 'syntactic': 0.16; 'class,': 0.16; 'classes,': 0.19; 'method.': 0.19; 'jan': 0.22; 'header:In-Reply-To:1': 0.22; 'subject:question': 0.22; 'converted': 0.23; 'pass': 0.27; 'looks': 0.28; 'class': 0.29; 'this.': 0.30; 'does': 0.31; "skip:' 10": 0.32; 'to:addr:python- list': 0.32; 'worry': 0.33; 'created': 0.33; 'operations': 0.33; 'test': 0.33; 'uses': 0.34; 'header:X-Complaints-To:1': 0.34; 'print': 0.35; 'header:User-Agent:1': 0.35; 'call,': 0.35; 'skip:o 20': 0.37; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'header:Received:5': 0.40; 'fact,': 0.60; 'skip:h 20': 0.60; 'happen': 0.61; 'back': 0.61; 'details': 0.64; 'special': 0.66; 'subject:about': 0.66; 'hand,': 0.72; "'object'": 0.84; '11:43': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: A question about Python Classes
Date Thu, 21 Apr 2011 13:39:17 -0400
References <2219ee53-e8aa-4ac4-839f-014c3d1b1914@a19g2000prj.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host rain.gmane.org
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9
In-Reply-To <2219ee53-e8aa-4ac4-839f-014c3d1b1914@a19g2000prj.googlegroups.com>
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.714.1303407571.9059.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 82.94.164.166
X-Trace 1303407572 news.xs4all.nl 81476 [::ffff:82.94.164.166]:51246
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3814

Show key headers only | View raw


On 4/21/2011 11:43 AM, chad wrote:
> 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?

When you ask for an attribute of an instance of a class, the attribute 
lookup first looks at the instance; if not there, then the class; if not 
there, then superclass(es); and so on back to class 'object'.

 >>> class C(): pass

 >>> c=C()
 >>> c.__hash__
<method-wrapper '__hash__' of C object at 0x00FCB5D0>

# how does this happen when C has no __hash__ method?

 >>> C.__hash__
<slot wrapper '__hash__' of 'object' objects>

# C inherits __hash__ and other special methods from 'object'

 >>> hash(c)
1035101

# uses the default, inherited method.

Most syntactic operations and builtins are ultimately converted to a 
special method call, often inherited like this. In fact, c.x is 
converted to object.__getattribute__(c, 'x').

 >>> object.__getattribute__(c, '__hash__')
<method-wrapper '__hash__' of C object at 0x00FCB5D0>

You do need to understand inheritance. On the other hand, do not worry 
about behind-the-scenes implementation details like 'method_wrapper' and 
'slot_wrapper' classes, which may be CPython specific.

-- 
Terry Jan Reedy

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