hash consing
John S. Yates, Jr.
john at yates-sheets.org
Sat Oct 27 11:22:51 EDT 2001
On Sat, 27 Oct 2001 07:21:15 -0700, "Emile van Sebille" <emile at fenx.com> wrote:
>
>"John S. Yates, Jr." <john at yates-sheets.org> wrote in message
>news:vh6lttk4mkchsk952re68udp4bqqvc1c18 at 4ax.com...
>> Is it possible to implement an "immutable" python
>> class that maintains a single instance of each unique
>> value via hash consing? I know that I can maintain
>> the hash table outside of the class, lookup the
>> argument tuple for the class instance I want and only
>> call the constructor when I the lookup fails. What
>> I would really like is to be able to hide that logic
>> inside the constructor.
>
>I'm not sure what consing means, but perhaps the code below helps you out.
It's a term that originated with pure (applicative) Lisp.
The simplest implementation of (cons x y) is to always
allocate a news cons cell. An alternative is to allocate
a new cell only when a pre-existing cell with contents
(x y) does not already exist. This latter approach is
usually referred to as "hash consing".
>The intent is that you shadow any cache friendly class with my_class =
>Cache(my_class), where friendly means a class without a __call__ method,
>with properly referenced class level attributes, and with hashable __init__
>args.
I had been trying to shadow the class's __init__ function.
Now I see that indeed what I really want is to shadow the
class itself.
>
>HTH,
Yes it does help. Thanks!
BTW. Before posting I did check the cookbook entries at
http://aspn.activestate.com/ASPN/Cookbook with no luck.
This seems like a generic enough pattern that it might
merit a submission.
/john
--
John Yates
40 Pine Street
Needham, MA 02492
781 444-2899
More information about the Python-list
mailing list