[pyar] Duda acerca de sets

Lucas Liendo mindmaster en gmail.com
Dom Ene 29 02:50:25 ART 2012


Guillermo, muchas gracias por la pronta respuesta !

2012/1/29 Tordek <kedrot en gmail.com>

> On 29/01/12 02:28, Lucas Liendo wrote:
>
>> En el programa de prueba para la lista (l) la comparacion se realiza
>> correctamente (ingresa a __eq__ como es esperado) y la lista posee
>> un unico elemento como debe ser. En el segundo caso intento hacer lo
>> mismo sobre un set (s) pero se puede ver que terminan ingresando los
>> 2 objetos a la coleccion, lo cual es incorrecto. ¿Sera necesario
>> redifinir algun metodo adicional?
>>
>
> Tenés que definir __hash__:
>
> def __hash__(self):
>    return hash(self.id)
>
> De http://docs.python.org/**library/sets.html<http://docs.python.org/library/sets.html>
> :
>
> "[...] the requirements for set elements are the same as those for
> dictionary keys; namely, that the element defines both __eq__() and
> __hash__()."
>
> --
> Guillermo O. «Tordek» Freschi. Programador, Escritor, Genio Maligno.
> http://tordek.com.ar :: http://twitter.com/tordek
> ______________________________**_________________
> pyar mailing list pyar en python.org.ar
> http://listas.python.org.ar/**listinfo/pyar<http://listas.python.org.ar/listinfo/pyar>
>
> PyAr - Python Argentina - Sitio web: http://www.python.org.ar/
>
> La lista de PyAr esta Hosteada en USLA - Usuarios de Software Libre de
> Argentina - http://www.usla.org.ar
>



-- 

Saludos,
Lucas.
   _
__("<
\__/
 ^^
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20120129/f168713e/attachment.html>


More information about the pyar mailing list