Descriptors and side effects

Rich Harkins rich at worldsinfinite.com
Mon Nov 5 15:16:59 EST 2007


Bruno Desthuilliers wrote:
> Which is easy to do with properties too.

True enough.  It's the caching of the return value that's the value add
of course.  ;)

> 
>>  After
>> it is applied, then the penalties for function call of the property and
>> the computation are wiped out once the second access is requested.
> 
> Agreed. But I wouldn't use such a scheme for mutable types - which are 
> still the common case.
> 

In many cases, yeah.  Though I use a lot of immutable stuff in some of
my pet projects and such.  ConstProperty is definitely not meant as a
replacement for property, only when something constant can be derived
from something else constant, especially when the derivation is expensive.

>> Now, in the original example, len() might be considered too little for
>> this use and should be just generated in the constructor "for free".
>> OTOH, that assumes that __len__ hasn't been overridden to do something
>> more complicated and time consuming.  If the antecedent object is
>> static, and the derivative consequent is also static,
> 
> You mean 'immutable', I assume...

Yeah, that's probably the better term.

[snip]

Again, I've used it quite a bit for various things and it's worked well
for the sort of thing the OP was requesting.  Of course, your mileage
may vary.  :)

Cheers!
Rich

PS: Sorry about the weird reposts.  Thunderbird chaos.




More information about the Python-list mailing list