A question about Python Classes

Ian Kelly ian.g.kelly at gmail.com
Fri Apr 22 15:40:59 EDT 2011


On Fri, Apr 22, 2011 at 7:49 AM, Kyle T. Jones
<onexpadREMOVE at evomeryahoodotyouknow.com> wrote:
>> 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...)

Yes, it looks up the attribute in the superclass tree at the time that
it's referenced, not at the time it's instantiated or at the time the
class is created.  So:

>>> class Base(object):
...     x = 5
...
>>> class Test(Base):
...     pass
...
>>> t = Test()
>>> t.x
5
>>> Test.x = 42
>>> t.x
42
>>> Base.x = 7
>>> del Test.x
>>> t.x
7

Or were you talking about something else?



More information about the Python-list mailing list