how to keep collection of existing instances and return one on instantiation

Diez B. Roggisch deets at nospam.web.de
Wed Oct 5 17:04:04 EDT 2005


Diez B. Roggisch wrote:
> 
>>> Read the comments. What you say is essentially the same - the data 
>>> matters, after all. What do you care if there are several instances
>>> around?
>>>
>> In my case it matters more that the objects are the same.
>>
>> For example I want set([Spam(1), Spam(2),
>> Spam(3)]).intersect(set([Spam(1), Spam(2)]) to contain two items instead
>> of 0.
>>
>> For this and many other reasons it's important that Spam(n) is Spam(n).
> 
> 
> Ah, ok. Well, you could always use the __hash__ method to ensure  that - 
> might be better anyway, because then _you_ define what equality means. 
> But YMMV.

And the __cmp__ or __eq__/__ne__ methdos of course....

Diez



More information about the Python-list mailing list