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

Diez B. Roggisch deets at nospam.web.de
Wed Oct 5 23:01:10 CEST 2005


>>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.

Diez



More information about the Python-list mailing list