Another method of lazy, cached evaluation.

Kamilche klachemin at comcast.net
Wed Jan 18 18:54:42 EST 2006


simonwittber at gmail.com wrote:

> Now, when the pic_1 and pic_2 attributes are accessed, they will return
> an Image instance, which is something different to which they were
> initially assigned. Is this kind of behavior bad form?

That's pretty good, I like it.

> Likewise, what
> do people think about raising an exception during an assignment
> operation?

I prefer to use the 'if key in dict' idiom, but I know some others
prefer your method.

I foresee 2 difficulties, neither earth shattering:

1. Speed might be an issue - on prior projects, my speed tests showed
that overriding getattr and setattr slowed the program down by quite a
bit.

2. I see you are caching the loaded picture, but I would choose a
different bag than __dict__. I would create a dict called cache in the
class to store it in, to avoid possible name conflicts where the stat
name has the same name as the png (which is unlikely, I realize.)

A potential enhancement would be to have some sort of cleanup, to where
if a picture is not being used by any assignment statement, it would
drop off. Or perhaps a 'force cleanup' where it clears out all the
cache, which would force the 'loadimage' routine to run again the next
time the picture is referenced.

Good job!

--Kamilche




More information about the Python-list mailing list