Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #3814
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: A question about Python Classes |
| Date | 2011-04-21 13:39 -0400 |
| References | <2219ee53-e8aa-4ac4-839f-014c3d1b1914@a19g2000prj.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.714.1303407571.9059.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar
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