[Baypiggies] identity cache / flywheel design pattern

Drew Perttula drewp at bigasterisk.com
Sat Aug 2 22:43:53 CEST 2008

Shannon -jj Behrens wrote:
>>>> identity_cache = {}
>>>> s1 = identity_cache.setdefault(s1, s1)
>>>> s2 = identity_cache.setdefault(s2, s2)
> Of course, in a real library, I'd use lazy references.  Can anyone
> comment on this trick?  Does it already exist in the library?  Is
> there a standard name for this besides the flywheel design pattern?
> Is that the correct name?  I might go looking to see if it's in the
> Cookbook, but I'm not even sure if that's the right name.
> Thanks,
> -jj
I think you've reinvented intern(), or something very close to it.

BTW the pattern name is 'flyweight', although I'm not sure if it's 
appropriate here. You don't have part of the data in a per-instance 
object and the rest shared in a flyweight object; your entire object is 
sharable. (But maybe that's still flyweight, just a really simple form 
of it.)

More information about the Baypiggies mailing list