[pyar] Bug o feature?

Pablo Codeiro cutrix2k en gmail.com
Dom Feb 20 04:23:23 ART 2011


Pregunta de ignorante nomas...

...Esto está mal hecho? O un feature?

Porqué motivo, al diseñar el func_defaults, python usa una tupla, y no una
lista? que ventajas acarrea? El comportamiento, al menos a simple vista, no
es el que uno espera, al menos sin conocimientos de como funciona
internamente...

El 14 de febrero de 2011 01:10, Rafael Carrascosa
<rafacarrascosa en gmail.com>escribió:

> Guau, buenisimo :D saber esto me va a salvar de una buena decena de
> horas de debugeo en el futuro,
> me doy por respondido y muy contento, gracias por las respuestas!
>
> 2011/2/14 Ale <peralta.alejandro en gmail.com>:
> > El día 14 de febrero de 2011 00:22, Rafael Carrascosa
> > <rafacarrascosa en gmail.com> escribió:
> >> Hola gente, habiendo superado el test de la sonsera con 3 personas
> >> consultadas ahora les pregunto a ustedes.
> >>
> >> Para este código: http://pastebin.lugmen.org.ar/6432
> >> Yo esperaria de output "[1,2]", pero tengo "[1, 2, 1, 2, 1, 2, 1, 2,
> >> 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]"
> >> Estoy casi convencido que no puede ser un bug del interprete, pero...
> >> ¿cual es la explicación "feature" de este comportamiento?
> >
> > No es un bug, es un comportamiento conocido, es por el x=[] en el
> > __init__ de la clase.
> >
> > proba esto:
> > In [20]: def blah(a=[]): a.append(1); return a
> >   ....:
> > In [21]: blah()
> > Out[21]: [1]
> > In [22]: blah()
> > Out[22]: [1, 1]
> > In [23]: blah()
> > Out[23]: [1, 1, 1]
> > In [24]: blah()
> > Out[24]: [1, 1, 1, 1]
> > In [25]: blah()
> > Out[25]: [1, 1, 1, 1, 1]
> > In [26]: blah()
> > Out[26]: [1, 1, 1, 1, 1, 1]
> > In [27]: blah()
> > Out[27]: [1, 1, 1, 1, 1, 1, 1]
> > In [28]: blah()
> > Out[28]: [1, 1, 1, 1, 1, 1, 1, 1]
> >
> >
> > En general se hace
> >
> > blah(a=None):
> >  if not a:
> >     a = []
> >  etc..
> >
> > El tema está en que el objeto, la lista vacia, se crea una sola vez en
> > el momento de la definición de la clase.
> >
> >
> http://stackoverflow.com/questions/4535667/python-list-should-be-empty-on-class-instance-initialisation-but-its-not-why
> >
> >
> >>
> >> Saludos!
> >>
> >> PD: python version 2.6.5 en un ubuntu 10.04 vainilla
> >> _______________________________________________
> >> pyar mailing list pyar en python.org.ar
> >> http://listas.python.org.ar/listinfo/pyar
> >>
> >> PyAr - Python Argentina - Sitio web: http://www.python.org.ar/
> >>
> >
> >
> >
> > --
> > Ale.
> > _______________________________________________
> > pyar mailing list pyar en python.org.ar
> > http://listas.python.org.ar/listinfo/pyar
> >
> > PyAr - Python Argentina - Sitio web: http://www.python.org.ar/
> >
> _______________________________________________
> pyar mailing list pyar en python.org.ar
> http://listas.python.org.ar/listinfo/pyar
>
> PyAr - Python Argentina - Sitio web: http://www.python.org.ar/
>
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20110220/a655fcb0/attachment.html>


More information about the pyar mailing list