[Edu-sig] Topics for CS2

Scott David Daniels Scott.Daniels at Acm.Org
Thu Feb 5 18:33:32 CET 2009


Scott David Daniels wrote:
> Scott David Daniels wrote:
> I wrote:
> 
>> In PythonOOP.doc (page 4) you say:
>>  > There are two pieces of "magic" that make line 3 work.  First, the
>>  > interpreter has to find the method set_vars.  It's not in cat2.  Then,
>>  > the instance cat2 has to be inserted as the first argument to set_vars.
>>  > The method is found by a search starting at cat2 ( the instance on
>>  > which the method is called ).  The search then proceeds to the parent
>>  > class, then to the grandparent, and so on up to object, the ancestor
>>  > of all Animal classes.  In this case, we find set_vars in Cat.
>>
>> For instances of subclasses of object (such as Cat), method lookups
>> start in Cat, not cat2.  If you try to set cat2.set_vars, you cannot
>> (because a method is a property stored in he class), and you will try
>> the set method of that property which will fail.  Simple class variables
>> are over-ridden by assignments to the instance values, but properties
>> are not.
     This should be read/write properties are not.
> And then immediately went to make a test case, which failed.  Which
> I should, of course, done before hitting send.  I retract my comment
> and will get back to you when I get my understanding straightened out.

And I just noticed a great thread on comp.lang.python addressing the
exact lookup order for obtaining attributes most recent post this
morning.  The Thread Title is "Understanding  Descriptors",
Brian Allen Vanderburg II asked the initial question, and Aahz and
Bruno Desthuillers came up with a thorough answer.
I'll put a link here, but it may wrap nastily.  Nickel summary, lookup
order is not dirt simple, and reading and writing work differently.

http://groups.google.com/group/comp.lang.python/browse_thread/thread/a136f7626b2a8b7d/70a672cf7448c68e

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Edu-sig mailing list