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


Groups > comp.lang.python > #3886

Re: A question about Python Classes

Date 2011-04-22 12:38 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: A question about Python Classes
References <2219ee53-e8aa-4ac4-839f-014c3d1b1914@a19g2000prj.googlegroups.com> <mailman.716.1303410257.9059.python-list@python.org> <ios100$b0e$2@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.760.1303500423.9059.python-list@python.org> (permalink)

Show all headers | View raw


Kyle T. Jones wrote:
> Ethan Furman wrote:
>> 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?
>>
>> You don't need to create an instance of BaseHandler.  You have the
>> class, Python knows you have the class -- Python will look there if the
>> subclasses lack an attribute.
>>
>> ~Ethan~
>>
> 
> Really?  That's not at all how I thought it worked in Python 
> (post-instantiation referencing of class and superclass code...)

I'm not sure exactly what you're asking/stating, but does this help?

8<---Py 3.2 code------------------------------------------
class BaseClass():
     def bFoo(self):
         print("Base foo here!")

class NormalClass(BaseClass):
     def nFoo(self):
         print("Normal foo here.")

class EnhancedClass(NormalClass):
     def eFoo(self):
         print("Enhanced foo comin' at ya!")

class EnrichedClass(EnhancedClass):
     def rFoo(self):
         print("Am I glowing yet?")

test = EnrichedClass()
test.bFoo()
test.nFoo()
test.eFoo()
test.rFoo()

def newFoo(self):
     print("Ha!  You've been replaced!")

BaseClass.bFoo = newFoo

test.bFoo()
8<----------------------------------------------------------

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